duanbei2914 2013-08-31 04:51
浏览 303
已采纳

Laravel 4 - 资源控制器添加组前缀而不更改路由名称

See this example:

    Route::group(array('prefix' => 'admin'), function()
    {
        //this resolves to admin/departments
        Route::resource('departments', 'DepartmentsAdminController');
        //this resolves to admin/users
        Route::resource('users', 'UsersAdminController');
        //this resolves to admin/companies  
        Route::resource('companies', 'CompaniesAdminController');
        Route::resource('projects', 'ProjectsAdminController');
        Route::resource('jobs', 'JobsAdminController');
        Route::resource('milestones', 'MilestonesAdminController');
        Route::resource('hours', 'HoursAdminController');
        Route::resource('notes', 'NotesAdminController');
        Route::resource('briefs', 'BriefsAdminController');
        Route::resource('brief_items', 'BriefItemsAdminController');
    });

Laravel changes route names based on the prefix in this case the prefix is admin

so all route names now prefixed with admin see:

    admin.users.create
    admin.users.edit

But what if i want to change the prefix to something else I will have to change the route names in my entire application.

What i want is to keep route name as is

    users.create
    users.edit        

and change the prefix without changing the route name.

Is there a way to keep the route names in resource controllers static and change the prefix anytime i want ?

  • 写回答

3条回答 默认 最新

  • dornc9470 2014-12-24 10:59
    关注

    you can follow the given code sample i use . by following this model, you can define route names as you prefer.

    Route::group( [ 'prefix' => 'admin' ], function(){
            Route::resource('pages', 'PageController', [
                'names' => [
                    'show' => 'page',
                    'edit' => 'page.edit'
                ],
                'only' => [
                    'show',
                    'edit'
                ]
    
            ]);
    
        });
    

    and then you can generate urls using .

    URL::route('page', array($id))
    

    for generating urls to /admin/page/{$id}

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大