3

我正在使用 Laravel 开发一个应用程序。但是我对应用程序的模块化结构有疑问。

这是一个例子。

假设我需要一个名为“特色项目”的模块;

这是一个小模块,应该包含在一些控制器中。

问题是我不知道该怎么做?这应该是一个将在其他控制器内部调用的新控制器吗?

这应该是模型吗?这应该写成一个捆绑包吗?

有谁知道什么是最好的方法?

4

4 回答 4

7

I dont think there's a right answer to this really as it is matter of preference though there are things to bear in mind to let you make an intelligent decision as to where your code should be placed.

Does the module have a small footprint and will function with no more than itself?

  • Create a library

Does the module require it's own routes and views but no requirement for libraries?

  • Create an application controller
  • Use Laravel Routes
  • Use bundle controllers

Does the module contain both the above?

  • Create a bundle with controllers and libraries

It's not as simplistic, other factors should be taken into consideration as well. If the module requires multiple controllers for example, you'd be better off writing it in a bundle. As bundles do not require you to have anything more than start.php file within the bundle directory, you register what you want with the Autoloader. From this you can see a lot of peoples preferences would be to build all their modular code in bundles.

于 2012-12-27T13:36:54.103 回答
1

这个包让你能够使用带有模块系统的 Laravel 5。您可以简单地将具有自己的控制器、模型、视图、翻译和路由文件的模块拖放或生成到 app/Modules 文件夹中,然后继续使用它们。

按照步骤

安装

安装此软件包的最佳方法是通过 Composer 通过您的终端。

从项目根目录运行以下命令

composer 需要 artem-schander/l5-modular 此操作完成后,只需将服务提供者添加到项目的 config/app.php 即可。

服务提供者

ArtemSchander\L5Modular\ModuleServiceProvider::class,

入门

内置的 Artisan 命令 php artisan make:module name [--no-migration] [--no-translation] 在 app/Modules 文件夹中生成一个随时可用的模块,并在必要时生成迁移/翻译。

https://github.com/Artem-Schander/L5Modular

于 2017-11-09T10:08:30.453 回答
0

把它放在一个库中并自动加载它怎么样?http://laravel.com/docs/models#libraries

这样您就可以在任何控制器中调用它。

于 2012-12-25T04:25:25.287 回答
0

诸如特色项目之类的东西可能会调用您的数据库。我会在您的产品模型中创建一个方法,例如称为 getFeaturedItems。然后使用 eloquent 或 fluent 来获得结果。

于 2012-12-25T06:11:02.857 回答