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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度