I'm trying to achieve the same, but also getting a NotFoundHttpException. If you do find a solution, please let me know.
I'm a little bit of a newbie to Laravel. However, I'm working through L5 a little at the moment.
I've been trying to adjust the router for a different purpose, but in a similar way. (I want to alter the $router->model() method to allow for slugs as well as ids).
Reviewing the code, the HTTP Kernel is passed the $router instance very early on, and it's no longer tied to the container's version. (line 49 of public/index.php).
$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
Then in 'handle', the kernel processes everything before passing to the router, (line 80, Illuminate/Foundation/Http/Kernel)
public function handle($request)
{
try
{
$response = $this->sendRequestThroughRouter($request);
}
catch (Exception $e)
{
$this->reportException($e);
$response = $this->renderException($request, $e);
}
$this->app['events']->fire('kernel.handled', [$request, $response]);
return $response;
}
A part of this is the dispatchToRouter method (line 208)
protected function dispatchToRouter()
{
return function($request)
{
$this->app->instance('request', $request);
return $this->router->dispatch($request);
};
}
As you can see, it's using the kernel's stored router - rather than the one we've set through the ServiceProvider.
As a temp fix to test, I've altered the method to:
protected function dispatchToRouter()
{
$this->router = $this->app['router'];
return function($request)
{
$this->app->instance('request', $request);
return $this->router->dispatch($request);
};
}
I'm sure there is a better way, but this may give some clues? It doesn't fix everything - for example Middlewares get run on the original router, not the one we use at the end in this test change.
@Keoghan, you actually brought me onto an idea. It seems I got it working now. Essentially what I did was override dispatchToRouter in App\Http\Kernel
.
Made a package out of it:
You can just add $app->singleton('router', 'App\Your\Router');
to bootstrap/app.php
.. Just make sure App\Your\Router
extends Illuminate\Routing\Router
@bonroyage - glad you found a solution. I also hacked into that file for a bit :)
@shnhrrsn - thanks, that was also recommended after I submitted a pull request to the Laravel team to tweak the HttpKernel to allow a service provider to override the router - declined due to small use case.
/bootstrap/app.php still a pretty clean way to resolve.
All solutions I found had problems with working with route middlewares. So I tried to cover all pitfalls in the article. Take a look. http://blog.lanin.me/2015/10/03/extending-default-laravel-router/.
Hope it will help.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community