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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵