Hello !
I want to use a custom UrlGenerator
implementation in my application. How it is possible ?
I tried different methods but none worked without updating the core...
Using the container seems the better idea but the name of the UrlGenerator
class is hardcoded in the RoutinServiceProvider
.
For me the right way is a container binding like :
$this->app->bind('Illuminate\Contracts\Routing\UrlGenerator', 'App\Library\Routing\UrlGenerator');
But for this method I need to change the registerUrlGenerator
method to :
protected function registerUrlGenerator()
{
$this->app['url'] = $this->app->share(function ($app) {
$routes = $app['router']->getRoutes();
// The URL generator needs the route collection that exists on the router.
// Keep in mind this is an object, so we're passing by references here
// and all the registered routes will be available to the generator.
$app->instance('routes', $routes);
$url = $app->make('Illuminate\Contracts\Routing\UrlGenerator', [$routes, $app->rebinding(
'request', $this->requestRebinder()
)
]);
$url->setSessionResolver(function () {
return $this->app['session'];
});
// If the route collection is "rebound", for example, when the routes stay
// cached for the application, we will need to rebind the routes on the
// URL generator instance so it has the latest version of the routes.
$app->rebinding('routes', function ($app, $routes) {
$app['url']->setRoutes($routes);
});
return $url;
});
}
The only solution that I found without hacking the framework is to duplicate the whole 'url' service binding function. I think it's a lot of duplication (the callback contains all the instantiation logic) which is not really useful...
Thanks for your help !
$this->app->bind('url', CustomUrlGenerator::class);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community