将文件夹中的Laravel控制器分组

问题描述:

要组织我的控制器,我想在文件夹中对它们进行排序.例如,我有一个管理面板,该面板使用许多我不想与其他控制器混淆的控制器.我将这些控制器移到了/controllers目录中的文件夹中.

To organize my controllers, I would like to sort them in folders. For example, I have an admin panel that utilizes many controllers that I don't want mixed up with the other controllers. I moved those controllers in a folder in my /controllers directory.

所以我的结构如下:

controllers /
    BaseController.php
    HomeController.php

    admin /    
       AdminController.php

现在我的管理员看起来像这样:

Now my admin controller looks like this:

namespace Admin;

class AdminController extends \BaseController {

    public function getHome() {

        return \View::make('admin.home');

    }

}

然后我可以为管理面板进行分组路由:

Then I can do a grouped route for my admin panel:

Route::group(['namespace' => 'Admin'], function() {

    Route::get('admin', ['as' => 'admin', 'uses' => 'AdminController@getHome']);

});

但这绝对没有错,我发现必须使用\在这些控制器中为每个类命名空间是很麻烦的.有没有办法消除在admin下这些控制器中每个类的命名空间的使用?例如,我不想键入\View::make(),我想输入View::make().

There's absolutely nothing wrong with this however, I find it a nuisance having to namespace every class in these controllers using \. Is there I way I can eliminate the use of having to namespace every class in these controllers under admin? For example, I don't want to type \View::make(), I want to have View::make().

好像您希望控制器类仍位于全局名称空间中,但希望能够将它们组织到文件夹中.

Looks like you want your controller classes to still be in the global namespace but want the ability to organize them into folders.

如果您查看composer.json,您会看到默认的controllers文件夹是由"classmap"直接自动加载到该文件夹​​的.因此,您可以将其他文件夹添加到列表中.像这样:

If you look in your composer.json, you'll see that the default controllers folder is autoloaded by "classmap" to the folder directly. So you may add additional folders to the list. Like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/controllers/admin", <-- additional folders
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
    ]
},

请注意,我已将"app/controllers/admin"添加到classmap数组中.您可以根据需要添加任意数量.然后执行composer dump-autoload.

Notice that I added "app/controllers/admin" into the classmap array. You can add as many as you want. Then do a composer dump-autoload.

执行此方法的另一种方法是修改您的app/start/global.php:

Another way of doing the same approach is to modify your app/start/global.php:

ClassLoader::addDirectories(array(

    app_path().'/commands',
    app_path().'/controllers',
    app_path().'/controllers/admin', // additional folders
    app_path().'/models',
    app_path().'/helpers',
    app_path().'/database/seeds',

));

AFAIK,这与修改composer.json文件具有相同的效果,除了添加新路径后无需执行任何composer命令之外.

AFAIK, this gives the same effect as modifying composer.json file, with exception that you do not need to do any composer commands after adding a new path.

请注意,如果您不希望代码变得太大,那么上面的整个答案将很有效.想象一下,必须以这种方式维护数十个控制器文件夹,同时也有类名冲突的风险.

Note that the whole answer above will work well if you do not expect your code to grow considerably larger. Imagine having to maintain tens of controller folders this way, also the risk of class names *ing.