如何在Laravel 5中构build模块化应用程序?

我想用模块来分割我的应用程序。 例如,将会有一个“核心”模块,包含基本的loginfunction,应用程序布局/格式(CSS等),用户pipe理和日记。

稍后,我可以创build其他模块,如联系人pipe理器,可以轻松地添加或从应用程序中删除。

应用程序导航中会有一些逻辑来确定哪些模块存在,并显示/隐藏到它们的链接。

我怎样才能做到这一点在目录结构,名称空间和其他所需的东西?


我正在看creolab / laravel-modules,但是它指出它是用于Laravel 4.我仍然可以用同样的方法使用它吗?

文档说在每个模块目录中放置模型,控制器和视图,但是这对于路由是如何工作的? 理想情况下,我希望每个模块都有自己的routes.php文件。 如何将所有这些工作与httpresources目录中的东西?


我正在想这样的事情:

模块的想法

但我不知道如何才能使其工作。


我刚刚在这里试过这个教程:

http://creolab.hr/2013/05/modules-in-laravel-4/

没有额外的图书馆等,只是纯Laravel 5。

我似乎碰到了一堵砖墙,并显示错误消息:

 FatalErrorException in ServiceProvider.php line 16: Call to undefined method Illuminate\Config\Repository::package() 

关于以下内容:

 <?php namespace App\Modules; abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider { public function boot() { if ($module = $this->getModule(func_get_args())) { $this->package('app/' . $module, $module, app_path() . '/modules/' . $module); } } public function register() { if ($module = $this->getModule(func_get_args())) { $this->app['config']->package('app/' . $module, app_path() . '/modules/' . $module . '/config'); // Add routes $routes = app_path() . '/modules/' . $module . '/routes.php'; if (file_exists($routes)) require $routes; } } public function getModule($args) { $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null; return $module; } } 

是什么原因造成的,我该如何解决?


现在让我更多地了解这一点。 得到我的包/模块路线和意见工作,这是伟大的:

 abstract class ServiceProvider extends \Illuminate\Support\ServiceProvider { public function boot() { if ($module = $this->getModule(func_get_args())) { include __DIR__.'/'.$module.'/routes.php'; } $this->loadViewsFrom(__DIR__.'/'.$module.'/Views', 'core'); } public function register() { if ($module = $this->getModule(func_get_args())) { } } public function getModule($args) { $module = (isset($args[0]) and is_string($args[0])) ? $args[0] : null; return $module; } } 

我还有最后一个问题,我将如何从我的包中加载所有控制器,就像loadViewsFrom()方法的工作方式一样?

我似乎已经想通了。

我会在这里发布它,以帮助其他初学者,这只是让命名空间正确。

在我的composer.json中我有:

 ... "autoload": { "classmap": [ "database", "app/Modules" ], "psr-4": { "App\\": "app/", "Modules\\": "Modules/" } } 

我的目录和文件结束了这样的:

在这里输入图像说明

我得到我的核心模块router.php通过包装我的控制器的模块在指定名称空间组:

 Route::group(array('namespace' => 'Modules\Core'), function() { Route::get('/test', ['uses' => 'TestController@index']); }); 

我想象一下,当我来为包做模型的时候,会得到正确的命名空间。

感谢您的帮助和耐心!

解:

第一步:在“app /”里面创build文件夹“模块”


第2步:在模块文件夹中创build您的模块(模块1(假设pipe理模块))

  Inside admin module : create the following folder 1. Controllers (here will your controller files) 2. Views (here will your View files) 3. Models (here will your Model files) 4. routes.php (here will your route code in this file) 

同样,您可以创build多个模块

 Module2( suppose API ) -Controllers -Views -Models -routes.php 

第3步:在“Modules /”文件夹中创buildModulesServiceProvider.php


第四步:在ModulesServiceProvider.php中粘贴下面的代码

 <?php namespace App\Modules; /** * ServiceProvider * * The service provider for the modules. After being registered * it will make sure that each of the modules are properly loaded * ie with their routes, views etc. * * @author kundan Roy <query@programmerlab.com> * @package App\Modules */ use Illuminate\Support\Facades\Route; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class ModulesServiceProvider extends ServiceProvider { /** * Will make sure that the required modules have been fully loaded * * @return void routeModule */ public function boot() { // For each of the registered modules, include their routes and Views $modules=config("module.modules"); while (list(,$module)=each($modules)) { // Load the routes for each of the modules if (file_exists(DIR.'/'.$module.'/routes.php')) { include DIR.'/'.$module.'/routes.php'; } if (is_dir(DIR.'/'.$module.'/Views')) { $this->loadViewsFrom(DIR.'/'.$module.'/Views',$module); } } } public function register() { } } 

第5步:在'config / app.php'文件中添加以下行

 App\Modules\ModulesServiceProvider::class, 

第六步:在“config”文件夹下创buildmodule.php文件

第7步:在module.php中添加以下代码(path =>“config / module.php”)

 <?php return [ 'modules'=>[ 'admin', 'web', 'api' ] ]; 

注意:您可以添加您的模块名称,无论您已经创build。 这里有模块。

第八步:运行这个命令

 composer dump-autoload 

有点晚,但如果你想在你的未来项目中使用模块,我写了一个模块生成器。 它通过php artisan make:module name生成模块php artisan make:module name您也可以将一些模块放在app/Modules文件夹中,并准备好使用/工作。 看一看。 节省一些时间;)

L5-模块化

你也可以使用pingpong-labs

文件在这里 。

这是一个例子。

你可以安装并检查过程。

注意:我不是广告。 只是检查了在模块支持上build立在Laravel上的cms。 所以认为这可能对你和其他人有帮助。

Kundan roy:我喜欢你的解决scheme,但是我从StackOverflow复制你的代码,我不得不改变引号和半引号使它工作 – 我认为SOF取代了这些。 还更改了base_path()的Dir,使其与Laravel的(新)格式更加一致。

 namespace App\Modules; /** * ServiceProvider * * The service provider for the modules. After being registered * it will make sure that each of the modules are properly loaded * ie with their routes, views etc. * * @author kundan Roy <query@programmerlab.com> * @package App\Modules */ use Illuminate\Support\Facades\Route; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider; class ModulesServiceProvider extends ServiceProvider { /** * Will make sure that the required modules have been fully loaded * @return void routeModule */ public function boot() { // For each of the registered modules, include their routes and Views $modules = config("module.modules"); while (list(,$module) = each($modules)) { // Load the routes for each of the modules if(file_exists(base_path('app/Modules/'.$module.'/routes.php'))) { include base_path('app/Modules/'.$module.'/routes.php'); } // Load the views if(is_dir(base_path('app/Modules/'.$module.'/Views'))) { $this->loadViewsFrom(base_path('app/Modules/'.$module.'/Views'), $module); } } } public function register() {} } 

pingpong/modules是一个使用模块pipe理你的大型应用程序的laravel包。 模块就像一个简单的结构的laravel包,它有一些视图,控制器或模型。

它正在Laravel 4和Laravel 5中工作。

要通过composer php进行安装,只需在composer.json文件中添加以下内容:

 { "require": { "pingpong/modules": "~2.1" } } 

然后运行composer install来获取包。

要创build一个新的模块,你可以简单地运行:

php artisan module:make <module-name>

– 必需。 模块的名称将被创build。 创build一个新的模块

php artisan module:make Blog

创build多个模块

php artisan module:make Blog User Auth

更多访问: https : //github.com/pingpong-labs/modules