4

I am wanting to structure my laravel app in a way that all of my code is under the src directory. My project structure would look something like the below. How would I do this where I can still call Route::get('accounting/item/{id}','AccountingItemController@getId')

I am wanting to avoid adding every module under src to the ClassLoader. Is there a way to tell the class loader to load all sub-directories under the parent directory src?

app
app/src
app/src/accounting
app/src/accounting/controllers
app/src/accounting/models
app/src/accounting/repos
app/src/accounting/interfaces
app/src/job
app/src/job/controllers
app/src/job/models
app/src/job/repos
app/src/job/interfaces
4

2 回答 2

11

是的,它被称为 PSR-0。

您应该命名所有代码。通常,您将拥有一个供应商名称,您将使用顶级命名空间。您的应用程序结构应该看起来像这样。

app/src/Vendor/Accounting/Controllers
app/src/Vendor/Job/Controllers

然后,您的控制器将被相应地命名。

namespace Vendor\Accounting\Controllers;

在路线中使用它们时。

Route::get('accounting/item/{id}','Vendor\Accounting\Controllers\ItemController@getId');

最后,您可以在composer.json.

"autoload": {
    "psr-0": {
        "Vendor": "app/src"
    }
}

当然,如果您不想要顶级Vendor命名空间,您可以将其删除,但您需要将每个组件注册为 PSR-0。

"autoload": {
    "psr-0": {
        "Accounting": "app/src",
        "Job": "app/src",
    }
}

完成后,运行composer dump-autoload一次,您应该能够添加新的控制器、模型、库等。只需确保目录结构与每个文件的命名空间一致。

于 2013-07-02T12:40:15.217 回答
1

你有没有安装作曲家?你应该使用这个:

composer dump-autoload

但是你可以将目录添加到 Laravel 的类加载器中。在此处查看参考:http: //laravel.com/api/class-Illuminate.Support.ClassLoader.html

于 2013-06-30T19:32:45.867 回答