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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?