How to structure a modular app in Laravel 5?

后端 未结 6 1331
生来不讨喜
生来不讨喜 2020-12-22 15:30

I would like to divide my application in modules. For instance, there would be a \"core\" modules that contains the basic login functionality, app layout/formatting (CSS etc

相关标签:
6条回答
  • 2020-12-22 15:53

    I seem to have figured it all out.

    I'll post it here in case it helps other beginners, it was just about getting the namespaces right.

    In my composer.json I have:

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

    My directory and files ended up like this:

    enter image description here

    I got my Core module router.php to work by wrapping my controllers for that module in a group specifying the namespace:

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

    I imagine when I come to doing my models for the package it will be a similar case of getting the namespaces right.

    Thanks for all your help and patience!

    0 讨论(0)
  • 2020-12-22 15:53

    Solution:

    Step1: Create Folder “Modules” inside “app/”


    Step2: In Modules folder create your Module (Module1( suppose admin Module))

     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)
    

    Similarly, you can create multiple modules

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

    Step3 : Create ModulesServiceProvider.php inside “Modules/” Folder


    Step4 : Paste following code inside 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() { }
    
    }
    

    Step5 : Add following line inside ‘config/app.php’ file

    App\Modules\ModulesServiceProvider::class,
    

    Step6 : Create module.php file inside ‘config’ folder

    Step7 : Add following code inside module.php (path => “config/module.php”)

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

    Note : You can add your module name whichever you have created. Here there are modules.

    Step8 : Run this command

    composer dump-autoload
    
    0 讨论(0)
  • 2020-12-22 15:55

    A little late, but if you want to use modules in your future projects, i've written a module generator. It generates modules via php artisan make:module name You can also just drop some modules in the app/Modules folder and they are ready to use/work. Take a look. Save some time ;)

    l5-modular

    0 讨论(0)
  • 2020-12-22 16:06

    You can also use pingpong-labs

    documentations Here.

    Here is an example.

    You can just install and check the process.

    Note: I am not advertising. Just checked that cms built on Laravel with module support. So thought that might be helpful for you and others.

    0 讨论(0)
  • 2020-12-22 16:08

    pingpong/modules is a laravel package which created to manage your large laravel app using modules. Module is like a laravel package for easy structure, it have some views, controllers or models.

    It's working in both Laravel 4 and Laravel 5.

    To install through composer, simply put the following in your composer.json file:

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

    And then run composer install to fetch the package.

    To create a new module you can simply run :

    php artisan module:make <module-name>

    - Required. The name of module will be created. Create a new module

    php artisan module:make Blog

    Create multiple modules

    php artisan module:make Blog User Auth

    for more visit: https://github.com/pingpong-labs/modules

    0 讨论(0)
  • 2020-12-22 16:15

    Kundan roy: I liked your solution but I copied your code from StackOverflow, I had to change the quotes and semi-quotes to get it working - I think SOF replace these. Also changed Dir for base_path() to be more inline with Laravel's (new) format.

    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() {}
    
    }
    
    0 讨论(0)
提交回复
热议问题