Abandoned
This package is no longer maintained., (*1)
I encourage you to use composer packages for your modules to get the most flexibility out of it., (*2)
Modules in Laravel 4
Application specific modules in Laravel 4 can be enabled by adding the following to your "composer.json" file:, (*3)
"require": {
"creolab/laravel-modules": "dev-master"
}
And by adding a new provider to your providers list in "app/config/app.php":, (*4)
'providers' => array(
'Creolab\LaravelModules\ServiceProvider',
);
Also you need to add your modules directory to the composer autoloader:, (*5)
"autoload": {
"classmap": [
"app/modules"
]
}
This also means you need to run "composer dump-autoload" every time you add a new class to your module., (*6)
By default you can add a "modules" directory in your "app" directory. So as an example this is a structure for one of my projects:, (*7)
app/
|-- modules
|-- auth
|-- controllers
|-- models
|-- views
|-- module.json
|-- content
|-- controllers
|-- models
|-- views
|-- module.json
|-- shop
|-- module.json
|-- system
|-- module.json
Note that every module has a "module.json" file, in which you can enable/disable the module. I plan on adding more meta data to these module definitions, but I need feedback as to what to put in there.
The first thing will probably be some kind of a bootstrap class., (*8)
If you want to have your modules in more that 1 directories you need to change the packages config file as following:, (*9)
'path' => array(
'app/modules',
'public/site',
'another/folder/containing/modules',
),
And don't forget to add those directories to your autoload list inside the composer.json file., (*10)
One of the available option is the order in which the modules are loaded. This can be done simply by adding the following to your module.json file:, (*11)
"order": 5
The order defaults to 0, so keep that in mind if you don't define it., (*12)
For now take a look at the example implementation, and please provide feedback ;), (*13)
https://github.com/bstrahija/laravel-modules-example, (*14)
Commands
There are 2 commands available through this package:, (*15)
php artisan modules
Which simply diplays all current modules depending on the mode set in the configuration. And:, (*16)
php artisan modules:scan
Which is only required if you have your modules setup in the "manifest" mode (see below).
This command scans the modules exactly like in the "auto" mode, but caches the results into a manifest file., (*17)
Optimization
By default the package scans the "modules" directory for "module.json" files. This is not the best solution way to discover modules, and I do plan to implement some kind of a caching to the Finder class.
To optimize the modules Finder even more you can publish the package configuration, and add the modules and definitions directly inside the configuration file by running:, (*18)
php artisan config:publish creolab/laravel-modules
And the editing the file "app/config/packages/creolab/laravel-modules/config.php".
You just need to change the "mode" parameter from "auto" to "manual", and list your modules under the "modules" key. An example of that is already provided inside the configuration., (*19)
Note for Manual mode with multiple paths : Laravel-Modules could not determine witch path to use. So please specify the folder containing the module you want to load. Like this example :, (*20)
'modules' => [
'app/modules' => [
'system' => ['enabled' => true],
],
'another/modules/path' => [
'pages' => ['enabled' => false],
'seo' => ['enabled' => true],
],
],
You can also add multiple module paths as an array, but do note that if a module has the same name, there will be problems., (*21)
Including files
You can also specify which files in the module will be automatically included. Simply add a list of files inside your module.json config:, (*22)
{
"include": [
"breadcrumbs.php"
]
}
There are some defaults set on which files will be included if they exist. Take a look at the latest config file, and republish the configuration if needed. By default these files will be included:, (*23)
'include' => array(
'helpers.php',
'filters.php',
'composers.php',
'routes.php',
'bindings.php',
'observers.php',
)
So you have the choice to either add your custom files to the global configuration, which will look for these files in every module, or you can set it up on a per module basis by adding it to the module.json file., (*24)
Service providers
A new addition is registering service providers for each module. Just add a line to your module.json file that looks something like this:, (*25)
"provider": "App\\Modules\\Content\\ServiceProvider"
These service provider classes work exactly like any other service provider added to your app/config/app.php configuration, so setup these classes by extending the \Illuminate\Support\ServiceProvider class and adding the appropriate methods., (*26)
You can also register multiple providers for every module by simply providing an array:, (*27)
"provider": [
"App\\Modules\\Content\\ServiceProvider",
"App\\Modules\\Content\\AnotherServiceProvider"
]
Keep in mind that you may have to run composer dump-autoload so you want get error on missing classes., (*28)
Modules Manifest
Another possible mode is "manifest" which basically writes a JSON manifest file in your Laravel storage directory that contains all the modules definitions.
This is only done the first time and to update the manifest file you need to either delete it, or rescan the modules via the following command:, (*29)
php artisan modules:scan
Assets
Just recently the ability to publish public assets for each module has been added. Just run:, (*30)
php artisan modules:publish
And all modules that contain an "assets" directory will be published to the Laravel public directory.
You can also publish assets for individual modules by providing the module name:, (*31)
php artisan modules:publish content
Migrations
Every module can have it's own migrations, and they need to be in the module/migrations directory. So for example if you want to create a migration that creates a user table for the auth module:, (*32)
php artisan migrate:make create_users_table --path=app/modules/auth/migrations --table=users --create
And to run all module migrations do this:, (*33)
php artisan modules:migrate
Or to run migrations for a specific module:, (*34)
php artisan modules:migrate auth
You can also seed the database form the module if your module.json contains a seeder setting. Just pass the --seed option to the command:, (*35)
php artisan modules:migrate --seed
Seeding
The modules can also have seeders. Just create the class like you would create a normal seeder, place it somewhere inside your module, and be sure to run composer dump-autoload. Then add the following to your module.json file:, (*36)
"seeder": "App\\Modules\\Content\\Seeds\\DatabaseSeeder"
This setting should contain the namespace path to your seeder class. Now simply run:, (*37)
php artisan modules:seed
To seed all your modules. Or you can do it for a specific module:, (*38)
php artisan modules:seed content
Commands
You can add module specific commands. This is a sample artisan command file creation :, (*39)
php artisan command:make <MyModuleCommandName> --path=app/modules/<MyModule>/commands --namespace=App\Modules\<MyModule>\Commands --command=modules.mymodule:mycommand
Then in the module.json add (you can also add an array if you have multiple commands) :, (*40)
"command": "App\\Modules\\<MyModule>\\Commands\\MyModuleCommandName"
After a dump-autoload you can now execute modules.mymodule:mycommand from command line :, (*41)
php artisan modules.mymodule:mycommand
Aliases
If you declare Facades into your modules you will like to create Aliases for your module, you can simply reference your alias in the module.json
:, (*42)
"alias": {
"<MyAlias>" "App\\Modules\\<MyModule>\\Facades\\<MyFacade>"
}
License
Thi package is open-sourced software licensed under the MIT license., (*43)