简体   繁体   English

Laravel 命名路由不起作用

[英]Laravel named routes not working

Recently I switched to Laravel 5.3.最近我切换到 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. UrlGenerator.php 第 314 行中的 InvalidArgumentException:路由 [auth.activate] 未定义。

It works perfectly fine with它与

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

Is this new in Laravel 5.3??这是 Laravel 5.3 的新功能吗?? I am fairly new to Laravel itself.我对 Laravel 本身还很陌生。

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(); Laravel 5.2 添加了 fluent 方法name($name)作为['as' => $name]的快捷方式替换,但是name($name)方法需要$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.在当前形式中,示例laravel/laravel包中实现的 RoutingServiceProvider 会在幕后为您处理此问题,但如果您以任何自定义方式加载路由,则需要在适当的时间触发刷新.

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请参阅https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Support/Providers/RouteServiceProvider.php#L47-L50了解它在 Laravel 8.x 中的处理方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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