Modular approach in Laravel Application
Laravel is a great framework, not only because of the elegant syntax, rapid development tool but also because of the ecosystem & open source packages available for it. These packages help a lot to reduce the development time.
Modules
Modules are similar to packages with there own Models, View, Controller, Migrations and other classes.
By default in a Laravel application all Controllers, Models goes inside app/ folder, Migrations, Seeder, Providers etc have there own folder.
As the application grows, these directories become cumbersome. Searching logics for a particular section of the application become difficult.
This is where going Modular approach for big projects make development &maintenancee easy. With Module,s you can create different modules for different sections of your application. Each modules with there independent confi, Controllers, Models, View, Migration, Seeder, providers etc.
To implement modules in Laravel you can just autoload the Modules folder using PSR-4 and be done with it. But this will involved more works like registering namespace for language, views, config, running migration etc.
Luckily there is a package which takes care of these and provides helper classes to manage module easily. Checkout Laravel Module
Laravel Modules
- Installing Laravel Modules is similar to installing any other package.
- Laravel Modules it provides Artisan Commands to create new Modules, activate/deactivate modules, create migrations. Below is a quick screenshot of different artisan command it provides.
- When you create a new module it also registers new custom namespace for Lang, View and Config.
12Lang::get('blog::group.name');@trans('blog::group.name');
- Apart from these it also provides useful Facade Methods, Module Methods
- And also can publish your modules similar to a package (document). To install a published module
laravel module-installer (https://github.com/joshbrw/laravel-module-installer) package should be installed and it will automate the module installation inside/Modules
folder.
How we used Laravel Modules?
- Initially, when we started working on a pos (point of sale) application we didn’t have an idea of creating different modules. But as the requirements increased to have a plug-play Restaurant extension for it, the idea of making of modular made more sense.
- So after creating and adding restaurant and many other optional modules, we added a setting for each business to enable or disable different modules.
- Module::all(); method was used to list different modules the application had.
- Each business can enable or disable modules for them as per there needs.
- We used a combination of Module:has(‘blog’); and business settings to check if a module is available & enabled.
- The Module (or extension or plugin) can be put in any other application to add the functionality.
No Comments