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