[英]What are some use cases of Named Routes in Laravel?
After reading the documentation , I still only have a vague idea of what Named Routes are in Laravel. 阅读文档之后 ,我对Laravel中的“命名路线”仍然只有一个模糊的想法。
Could you help me understand? 你能帮我理解吗?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController@show')->name('profile');
It says: 它说:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
为给定的路由分配名称后,可以在生成URL或通过全局路由功能重定向时使用该路由的名称
I don't understand what the second part of the sentence means, about generating URLs or redirects. 我不明白句子第二部分关于生成URL或重定向的含义。
What would be a generated URL in the case of profile
from the above example? 对于上面示例中的
profile
,生成的URL是什么? How would I use it? 我将如何使用它?
After adding a name to a route, you can use the route()
helper to create urls. 将名称添加到路由后,可以使用
route()
帮助器创建URL。 This can now be used in your application. 现在可以在您的应用程序中使用它。
For instance, in your blade templates this may look like: 例如,在您的刀片模板中,它可能类似于:
{{ route('profile') }}
This will use the application url and the route path to create a url. 这将使用应用程序URL和路由路径来创建URL。
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes 最好的资源就在这里: https : //laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. 在您看来,一种常见的用例是。 Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
假设您的发布请求转到特定的路线,基本上没有命名的路线,您可以像这样简单地存储任务
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route. 但是说,例如,您需要将路由更新到/ task / store,则需要在使用该路由的所有位置进行更新。
But consider you used a named route 但是请考虑您使用了命名路线
Route::post('/task', 'TaskController@store')->name('task.store');
With named routes you can use the route like this in your view: 使用命名路线,您可以在视图中使用以下路线:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need. 现在,如果您选择更新路线,则只需更改路线文件并将其更新为所需的内容即可。
Route::post('/task/now/go/here', 'TaskController@store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this: 如果您需要将参数传递给路由,可以将其作为参数传递给路由助手,如下所示:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating. 给出所有使用刀片模板的视图示例。
this is how it looks it: 这是它的外观:
named route sample name('store');
命名路线样本
name('store');
: :
Route::get('/store-record','YourController@function')->name('store');
store
is the named route here. store
是这里的命名路线。 to call it use route('store')
用
route('store')
称呼它
defining another type of route. 定义另一种路线。 this is not named route:
这不是命名路线:
Route::get('/store-record','YourController@function')
you can access this route using {{ url('/store-record') }}
您可以使用
{{ url('/store-record') }}
访问此路线
hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.