Custom Request object for specific routes
I'm adding an ecommerce application into an existing Laravel install via service providers, have kept the two things to their own domains. However, I would like the Store\Core\Request object to have a storeResolver which will allow $request->store() in a similar way to the the $request->user() or $request->route() methods.
I have achieved this through extending the request object and adding my own methods that mirror those used by the aforementioned methods:
namespace Store\Core\Http;
use Closure;
use Illuminate\Http\Request as BaseRequest;
class Request extends BaseRequest
{
protected $storeResolver;
public function store()
{
return call_user_func($this->getStoreResolver());
}
public function getStoreResolver()
{
return $this->storeResolver ?: function () {
};
}
public function setStoreResolver(Closure $callback)
{
$this->storeResolver = $callback;
return $this;
}
}
Setting a resolver during in the ServiceProvider:
/**
* Register a resolver for the current store.
*
* @return void
*/
protected function registerRequestRebindHandler()
{
$this->app->rebinding('request', function ($app, $request) {
$request->setStoreResolver(function () use ($app) {
return $app['store.resolver']();
});
});
}
and updating index.php like:
$response = $kernel->handle(
$request = Store\Core\Http\Request::capture()
);
This works, but now every request across the entire application (pre-existing and the new store) is a Store request object, masquerading as an Illuminate/Http/Request.
Once we reach the controllers and type-hint the Request object we get some strange behaviours. If I chose to type hint the Store/Http/Request object, non of the resolvers are defined (Store, User or Route). By type hinting the standard Illuminate/Http/Request object, everything works fine.. even though the initial request from index.php is a Store/Request.
I believe this is due to the rebinding process in the ServiceProvider? So, as it is.. it works but its not what I deem as don properly. Because of this type-hinting, the IDE has no knowledge of the storeResolver that is available and as a long-time Laravel user, I wouldn't expect it to be there.. it appears for all intents and purpose to be a standard request object when we know it's not.
In a perfect world I'd like to be able to type-hint my requests inside the Store project/namespace as Store/Http/Request, thus my team are aware that it is a different request object. Whilst, the existing application retains its original hints and usage of the Illuminate Request.
I tried to rebuild the Request via the Request::setFactory() method but that didn't work.
So with all the background in place, long story short - does anyone know of a way that I can have a service use a specific request object and not the base Illuminate one? Preferably without changing the index.php from the its default state and retaining all of the existing resolvers and functionality of the base request.
A route example:
GET /some/existing/uri - Illuminate/Request
GET /store/new/uri - Store/Request
Please or to participate in this conversation.