vendredi 23 octobre 2015

How to extend UrlGenerator to generate links with trailing slashes?

I've modified the htaccess file to force the trailing slash and would now like any url generation within Laravel to automatically add the slash so we don't have so many redirects.

# Force trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]

I then created a CustomUrlGenerator class which extends Illuminate\Routing\UrlGenerator and I am trying to override the toRoute function...

public function toRoute($route, $parameters, $absolute)
{
    $route = parent::toRoute($route, $parameters, $absolute);

    return $route . '/';
}

and in AppServiceProvider.php, I've added the following code...

$this->app->bind('url', function($app) {
    return new CustomUrlGenerator($app['routes']->getBindings(), request());
});

Unfortunately, I think this is breaking the app somehow because whenever I try to use anything which uses this function, it can not find the routes, probably because I'm setting this up too early in the life cycle.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire