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

时间:2015-02-12 19:04:04

标签: php laravel laravel-routing laravel-5

我想将我的应用程序分成模块。例如,会有一个"核心"模块,包含基本登录功能,应用程序布局/格式(CSS等),用户管理和日记。

稍后我可能会创建其他模块,例如可以轻松添加或从应用程序中删除的联系人管理器。

应用程序导航中会有一些逻辑用于确定存在哪些模块以及显示/隐藏指向它们的链接。

我如何在目录结构,命名空间和其他任何需要的方面做到这一点?


我正在研究creolab / laravel-modules,但它表示它适用于Laravel 4.我能以完全相同的方式使用它吗?

文档说在每个模块目录中放置模型,控制器和视图,但这如何与路由一起使用?理想情况下,我希望每个模块都有自己的routes.php文件。所有这些如何与httpresources目录中的内容一起使用?


我在考虑这样的事情:

Module idea

但我不知道如何让它发挥作用。


我刚试过这个教程:

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()方法的工作方式一样?

6 个答案:

答案 0 :(得分:34)

我似乎已经全力以赴。

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

在我的composer.json中,我有:

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

我的目录和文件最终结果如下:

enter image description here

我通过在指定命名空间的组中包装我的模块控制器来使用我的Core模块router.php:

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

我想,当我为包创建模型时,这将是一个类似的例子,可以正确地命名空间。

感谢您的帮助和耐心!

答案 1 :(得分:9)

解决方案:

  

步骤1:在“app /”

中创建“模块”文件夹
  

Step2:在Modules文件夹中创建模块(Module1(假设管理模块))

 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)

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

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

步骤3:在“Modules /”文件夹

中创建ModulesServiceProvider.php
  

步骤4:在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
 * i.e. 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,
  

步骤6:在'config'文件夹

中创建module.php文件      

步骤7:在module.php中添加以下代码(path =&gt;    “配置/ module.php”)

<?php

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

注意:您可以添加您创建的模块名称。这里有模块。

  

步骤8:运行此命令

composer dump-autoload

答案 2 :(得分:6)

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

l5-modular

答案 3 :(得分:2)

您也可以使用pingpong-labs

文件Here

Here is an example.

您只需安装并检查流程即可。

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

答案 4 :(得分:2)

Kundan roy:我喜欢你的解决方案,但是我从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
* i.e. 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() {}

}

答案 5 :(得分:0)

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

它在Laravel 4和Laravel 5中都有效。

要通过编辑器安装,只需将以下内容放在composer.json文件中:

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

然后运行composer install来获取包。

要创建新模块,您只需运行:

php artisan module:make <module-name>

- 必填。将创建模块的名称。 创建一个新模块

php artisan module:make Blog

创建多个模块

php artisan module:make Blog User Auth

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