dongzhuo5185 2014-05-14 18:37
浏览 189
已采纳

Laravel 4,组前缀需要更改路由名称。 真?

So I've been working on an app that lived on the domain root and now has to work on /admin. So URLs like domain.com/[resource] should now be domain.com/admin/[resource]. I didn't thought this very well before since I assumed that this had to be a very easy fix on Laravel. After all, that's one of the main reasons for not hardcoding routes, right?

So my routes.php file looked something like:

Route::group(['before' => 'auth'], function() {
    Route::resource('books', 'BooksController');
    ... more resources here ...
});

Going through the docs I found that 'prefix' => 'admin' would do the trick:

Route::group(['prefix' => 'admin', 'before' => 'auth'], function() {
    Route::resource('books', 'BooksController');
    ... more resources here ...
});

But it turns out that every route name get's changed from books.{action} to admin.books.{action} which requires me to change the whole app. Regexing would be dangerous and doing it manually would be annoying. Laravel was supposed to help with this! Or am I missing something?

  • 写回答

1条回答 默认 最新

  • doufang1954 2014-05-14 18:46
    关注

    This is untested, but after looking on the documentation for resource controllers it seems as though you can manually set their names. I'm assuming Laravel automatically namespaces grouped resource controller routes to avoid name collision, but you can override this to avoid going back through the rest of your app (just beware of future name collision):

    Route::resource(
        'books',
        'BooksController',
        array(
            'names' => array(
                'index'   => 'photo.index',
                'create'  => 'photo.create',
                'store'   => 'photo.store',
                'show'    => 'photo.show',
                'edit'    => 'photo.edit',
                'update'  => 'photo.update',
                'destroy' => 'photo.destroy',
            )
        )
    );
    

    Shorter Method:

    Just define a quick method at the top of your routes.php file to shorten up this repetitive task of creating an array of route names. Still not the greatest solution, but I believe its the only thing you can do with how Laravel has this set up.

    function createRouteNames($resource) {
        $names = array();
        $types = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
        foreach($types as $type) {
            $names[$type] = $resource . '.' . $type;
        }
        return $names;
    }
    
    Route::resource('books', 'BooksController', ['names' => createRouteNames('books')]);
    

    Note: [] === array() and may not be supported on older PHP's, meaning you may need to replace them with the old syntax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题