简体   繁体   中英

Laravel named routes not working

Recently I switched to Laravel 5.3.

I have the following route

Route::get('/activate/token', 'AccountActivationController@activate')->name('auth.activate');

But, when I use

dd(route('auth.activate'));

I get the following error:

InvalidArgumentException in UrlGenerator.php line 314: Route [auth.activate] not defined.

It works perfectly fine with

Route::get('/activate/token', [
    'as' => 'auth.activate',
    'uses' => 'AccountActivationController@activate',
]);

Is this new in Laravel 5.3?? I am fairly new to Laravel itself.

Thank You.

Missing a tick mark Try

dd(route('auth.activate'));

instead.

For anyone that comes across this in the future, it is because you aren't refreshing the name lookup on the router.

Laravel 5.2 added the fluent method name($name) as a shortcut replacement for ['as' => $name] , but the name($name) method requires $router->getRoutes()->refreshNameLookups(); to be called at some point after your routes are registered in order to actually complete that mapping internally.

In it's current form, the RoutingServiceProvider implemented in the example laravel/laravel package handles this for you behind the scenes, but if you're loading routes in any sort of custom way, you'll need to trigger that refresh yourself at an appropriate time.

See https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php#L47-L50 for how it's handled in Laravel 8.x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM