Organize Laravel projects with L5Modular
app/Modules directory. The package provides a set of artisan make commands to streamline the generation of these modular components.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.
app/Modules directory. The package provides a set of artisan make commands to streamline the generation of these modular components.config/modules.php path using the Artisan vendor publish command. When prompted, select the provider ArtemSchander\L5Modular\ModuleServiceProvider.php artisan vendor:publish::) 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');// File: App/Modules/HelloWorld/Services/FancyService.php
namespace App
Modules
HelloWorld
Services;
class FancyService
{
public static function doFancyStuff() {
return 'some output';
}
}Install the L5Modular package by running the composer require command from your project root.
Requirements for v2:
(Note: L5Modular v1 supports older PHP and Laravel versions).
composer require artem-schander/l5-modularspecific 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');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',
],
];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' ],
],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',
],
],
],enabled key to false within the specific configuration block for that module.'specific' => [
'HelloWorld' => [
'enabled' => false,
],
]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');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