dptt66700 2019-06-19 14:41
浏览 144
已采纳

Laravel中命名路由的一些用例是什么?

After reading the documentation, I still only have a vague idea of what Named Routes are in 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

I don't understand what the second part of the sentence means, about generating URLs or redirects.

What would be a generated URL in the case of profile from the above example? How would I use it?

  • 写回答

3条回答 默认 最新

  • douke6857 2019-06-19 14:49
    关注

    The best resource is right here : 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.

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么