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 ghpython这里总是报错而且rhino视图窗口内不显示怎么办
  • ¥15 布尔运算推导,请证明
  • ¥15 有没有人会打学生成绩管理系统呀
  • ¥15 在使用Fiddler和夜神模拟器抓包的时候一直出现443该怎么办啊QAQ搜了好几个笔记都没有解决
  • ¥15 3x7的二维数组A、B、C,A中的任意1个数组元素与B的任意1个数组元素、同时又与C的任意1个数组元素比较,把不同位置出现相同数的比较称为无意义,反之称为有意义,把有意义的比较打印输出。
  • ¥20 预测模型怎么处理原始数据(随机森林)
  • ¥20 请问discuz3.5如何实现插入ckplayer全能播放器功能呢?
  • ¥15 thingsboard代码编译出错误
  • ¥15 博途v18仿真报错怎么解决
  • ¥15 欧姆龙plc枕式包装机 ST编程