L5Modular Documentation

website·Indexed 30 days ago

https://l5modular.github.io/

A package for organizing Laravel projects into reusable modules. It provides a modular structure for application components and extends standard Laravel make commands with a --module option to generate controllers, models, jobs, and events within specific modules.

Tokens
3.3K
Snippets
37
Records
38
Agent score
75%

What's inside L5Modular

  1. Organize Laravel projects with L5Modular

    L5Modular enables a modular architecture in Laravel by allowing developers to group controllers, models, views, and routes into dedicated modules. These modules are stored and managed within the app/Modules directory. The package provides a set of artisan make commands to streamline the generation of these modular components.
  2. Publish the L5Modular configuration file

    To customize the behavior of the L5Modular package, you must publish the configuration file to your project's config/modules.php path using the Artisan vendor publish command. When prompted, select the provider ArtemSchander\L5Modular\ModuleServiceProvider.
    php artisan vendor:publish
  3. Render views and translations from a module

    To access views or translation strings located within a specific module, use the double-colon (::) syntax to specify the module name.
    // Render a view from the HelloWorld module
    return view('HelloWorld::welcome');
    
    // Access a translation from the HelloWorld module
    echo trans('HelloWorld::example.welcome');
  4. Load additional classes into a module

    L5Modular follows the PSR-4 autoloading standard. To add additional classes (e.g., Services) to a module, create the file in the desired directory within the module and ensure the filename matches the class name and the namespace is correct.
    // File: App/Modules/HelloWorld/Services/FancyService.php
    namespace App
    Modules
    HelloWorld
    Services;
    
    class FancyService
    {
        public static function doFancyStuff() {
            return 'some output';
        }
    }
  5. Install L5Modular via Composer

    Install the L5Modular package by running the composer require command from your project root.

    Requirements for v2:

    • PHP 7.2 or higher
    • Laravel 5.7 or higher

    (Note: L5Modular v1 supports older PHP and Laravel versions).

    composer require artem-schander/l5-modular
  6. Define and access custom configuration values for modules

    You can add arbitrary custom configuration keys to either the specific section of config/modules.php or a module's local config.php file. These custom values are accessible via the Laravel config() helper using two different paths: the full package path or the short module name path.
    // app/Modules/HelloWorld/config.php
    return [
        'enabled' => true,
        'custom' => 1337,
    ];
    
    // Accessing the value in code:
    $value = config('modules.specific.HelloWorld.custom');
    // OR
    $value = config('HelloWorld.custom');
  7. Configure individual modules using a local config.php file

    You can define module-specific settings by creating a config.php file in the root directory of a module (e.g., app/Modules/HelloWorld/config.php). This allows you to control whether the module is enabled, define its routing style, and customize its folder structure.

    Note: Settings in the local config.php have lower priority than settings defined in the specific section of the global config/modules.php file. If both exist, they are merged, and the global configuration takes precedence.

    return [
        'enabled' => false,
        'routing' => [ 'simple' ],
        'structure' => [
            'controllers' => 'Controllers',
            'views' => 'Views',
            'translations' => 'Translations',
        ],
    ];
  8. Configure default module routing

    Define the types of route files generated and loaded for all modules via config('modules.default.routing'). Supported options are:

    • web: Generates web.php with a predefined resource route; applies 'web' middleware and the module's controller namespace.
    • api: Generates an empty api.php; applies 'api' middleware and the module's controller namespace.
    • simple: Generates routes.php with a predefined resource route; applies the module's controller namespace.
    'default' => [
        'routing' => [ 'web', 'api' ],
    ],
  9. Apply configuration overrides for specific modules

    You can override default settings for individual modules using the config('modules.specific') array in config/modules.php or by placing a config.php file in the root of the specific module. The keys in the specific array must exactly match the module names. You can override enabled status, routing, and structure.
    'specific' => [
        'HelloWorld' => [
            'enabled' => false,
            'routing' => [ 'simple' ],
            'structure' => [
                'controllers' => 'Controllers',
                'models' => 'Entities',
                'views' => 'Views',
            ],
        ],
    ],
  10. Disable a specific module

    To disable a module and prevent it from being loaded by the service provider, set the enabled key to false within the specific configuration block for that module.
    'specific' => [
        'HelloWorld' => [
            'enabled' => false,
        ],
    ]
  11. Manage module status using the L5Modular Facade

    The L5Modular Facade provides methods to check the existence and status of modules, as well as toggle their state during runtime. Note that enable() and disable() only affect the current execution runtime; for permanent changes, use the specific array in config/modules.php.
    // Check if a module exists
    L5Modular::exists('HelloWorld');
    
    // Check if a module is enabled/disabled
    L5Modular::enabled('HelloWorld');
    L5Modular::disabled('HelloWorld');
    
    // Enable or disable a module for the current runtime
    L5Modular::enable('HelloWorld');
    L5Modular::disable('HelloWorld');
  12. Understand the L5Modular module directory structure

    A default generated module follows a structured directory layout within app/Modules/{ModuleName}. It typically includes folders for Http Controllers, Models, Resources (lang/views), and Routes (api/web).

    Modules can be customized to include additional directories such as Events, Jobs, Listeners, Mail, Notifications, Observers, Rules, and Database (factories/migrations/seeds), provided they follow the PSR-4 autoloading convention.

    laravel-project/
        app/
        └── Modules/
            └── HelloWorld
                ├── Http
                │   └── Controllers
                │       └── HelloWorldController.php
                ├── Models
                │   └── HelloWorld.php
                ├── resources
                │   ├── lang
                │   │   └── en.php
                │   └── views
                │       └── welcome.blade.php
                └── routes
                    ├── api.php
                    └── web.php