将名称添加到 Laravel 路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41532879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add name to laravel route
提问by Fabiotk
I've got a route defined as:
我有一个路由定义为:
Route::get('/novanoticia', 'HomeController@getNovaNoticia');
When I run php artisan route:list
it shows nothing on the name column. How to add a name to the route in such a way that I can later call it just by its name like: href="{{ route('route_name', $routeparam) }}
?
当我运行时,php artisan route:list
它在名称列上没有显示任何内容。如何以这样的方式向路由添加名称,以便我以后可以仅通过其名称来调用它,例如:href="{{ route('route_name', $routeparam) }}
?
Or will I have to redefine the route? Thanks in advance
还是我必须重新定义路线?提前致谢
回答by Alexey Mezenin
Option 1
选项1
Route::get('/novanoticia', 'HomeController@getNovaNoticia')->name('route_name');
Option 2
选项 2
Route::get('/novanoticia', ['as' => 'route_name', 'uses' => 'HomeController@getNovaNoticia']);