Significant Role of Plug and Play Module System in Laravel
A plug-and-play module in Laravel is a single module that de-clutters the codebase. It facilitates the domain boundary and distinguishes business logic such as payment, inventory, and users.
Moreover, it also aligns with the healthy practice in Laravel, as it promotes keeping logic out of controllers and aligns with the particular service classes.
Plug-and-play offers a plethora of convenience that makes it a boon for Laravel developers.
Why Go For a Plug-and-Play Model?
Making changes in code feels like playing a minesweeper game, one wrong click and the entire logic might blow up. This is where plug-and-play acts as an interwoven but independent model that handles a specific domain.
It also facilitates:
- Scalability, as adding the new feature or component becomes swift without having to rewrite the entire code.Β
- Instead of conducting the test of the entire application, you can run isolation module tests.
- If a particular module gets hit with a particular bug, it can be mitigated individually.
- With the bifurcation of modules, the team can work on different modules and steadily pace up the development process.
- And with clear, precise modules, updating and maintaining the application becomes manageable.
Building Your First Application
Hereβs the walkthrough on how you can set up your first individual module on your next project. In this example, we will be using RCV Core as a library package.
The biggest advantage of RCV Core is that it will preload the entire structure with one command.
php artisan module:make {name}
Setting Modular Architecture
While working on Laravel, whenever you choose a particular package, the structure should be something like this.
π modules/
βββ π UserManagement/
Β Β Β Β βββ π module.jsonΒ Β Β Β Β Β Β Β Β Β # Module configuration
Β Β Β Β βββ π composer.jsonΒ Β Β Β Β Β Β Β Β # Dependencies
Β Β Β Β βββ π src/
Β Β Β Β β Β βββ π Config/
Β Β Β Β β Β β Β βββ π config.phpΒ Β Β Β Β Β # Module-specific config
Β Β Β Β β Β βββ π Console/
Β Β Β Β β Β β Β βββ π Commands/ Β Β Β Β Β Β # Artisan commands
Β Β Β Β β Β βββ π Database/
Β Β Β Β β Β β Β βββ π Factories/Β Β Β Β Β Β # Model factories
Β Β Β Β β Β β Β βββ π Migrations/ Β Β Β Β Β # Database migrations
Β Β Β Β β Β β Β βββ π Seeders/Β Β Β Β Β Β Β # Database seeders
Β Β Β Β β Β βββ π Events/
Β Β Β Β β Β β Β βββ π UserCreated.php Β Β Β # Domain events
Β Β Β Β β Β β Β βββ π UserUpdated.php
Β Β Β Β β Β βββ π Exceptions/
Β Β Β Β β Β β Β βββ π UserNotFoundException.php
Β Β Β Β β Β β Β βββ π UserValidationException.php
Β Β Β Β β Β βββ π Http/
Β Β Β Β β Β β Β βββ π Controllers/
Β Β Β Β β Β β Β β Β βββ π UserController.php
Β Β Β Β β Β β Β β Β βββ π Api/ApiUserController.php
Β Β Β Β β Β β Β βββ π Middleware/
Β Β Β Β β Β β Β β Β βββ π UserAccessMiddleware.php
Β Β Β Β β Β β Β βββ π Requests/
Β Β Β Β β Β β Β β Β βββ π CreateUserRequest.php
Β Β Β Β β Β β Β β Β βββ π UpdateUserRequest.php
Β Β Β Β β Β β Β βββ π Resources/
Β Β Β Β β Β β Β Β Β βββ π UserResource.php
Β Β Β Β β Β β Β Β Β βββ π UserCollection.php
Β Β Β Β β Β βββ π Jobs/
Β Β Β Β β Β β Β βββ π SendWelcomeEmail.php Β # Background jobs
Β Β Β Β β Β β Β βββ π ProcessUserData.php
Β Β Β Β β Β βββ π Listeners/
Β Β Β Β β Β β Β βββ π SendWelcomeNotification.php
Β Β Β Β β Β β Β βββ π LogUserActivity.php
Β Β Β Β β Β βββ π Models/
Β Β Β Β β Β β Β βββ π User.php Β Β Β Β Β Β Β # Eloquent models
Β Β Β Β β Β β Β βββ π UserProfile.php
Β Β Β Β β Β βββ π Notifications/
Β Β Β Β β Β β Β βββ π UserWelcomeNotification.php
Β Β Β Β β Β β Β βββ π UserPasswordReset.php
Β Β Β Β β Β βββ π Observers/
Β Β Β Β β Β β Β βββ π UserObserver.php Β Β Β # Model observers
Β Β Β Β β Β βββ π Policies/
Β Β Β Β β Β β Β βββ π UserPolicy.php Β Β Β Β # Authorization policies
Β Β Β Β β Β βββ π Providers/
Β Β Β Β β Β β Β βββ π UserServiceProvider.php # Service provider
Β Β Β Β β Β β Β βββ π RouteServiceProvider.php
Β Β Β Β β Β β Β βββ π EventServiceProvider.php
Β Β Β Β β Β βββ π Repositories/
Β Β Β Β β Β βββ βββ π Interfaces/
Β Β Β Β β Β β Β βββ βββ π UserRepositoryInterface.php
Β Β Β Β β Β β Β βββ βββ π UserServiceInterface.php
Β Β Β Β β Β β Β βββ π UserRepository.php Β Β # Data access layer
Β Β Β Β β Β β Β βββ π UserProfileRepository.php
Β Β Β Β β Β βββ π Resources/
Β Β Β Β β Β β Β βββ π assets/
Β Β Β Β β Β β Β β Β βββ π css/
Β Β Β Β β Β β Β β Β βββ π js/
Β Β Β Β β Β β Β β Β βββ π images/
Β Β Β Β β Β β Β βββ π lang/
Β Β Β Β β Β β Β β Β βββ π en/
Β Β Β Β β Β β Β β Β βββ π es/
Β Β Β Β β Β β Β βββ π views/
Β Β Β Β β Β β Β Β Β βββ π layouts/
Β Β Β Β β Β β Β Β Β βββ π pages/
Β Β Β Β β Β β Β Β Β βββ π components/
Β Β Β Β β Β βββ π Routes/
Β Β Β Β β Β β Β βββ π web.phpΒ Β Β Β Β Β Β Β # Web routes
Β Β Β Β β Β β Β βββ π api.phpΒ Β Β Β Β Β Β Β # API routes
Β Β Β Β β Β β Β βββ π console.phpΒ Β Β Β Β Β # Console routes
Β Β Β Β β Β βββ π Services/
Β Β Β Β β Β β Β βββ π UserService.phpΒ Β Β Β # Business logic layer
Β Β Β Β β Β β Β βββ π UserNotificationService.php
Β Β Β Β β Β βββ π Tests/
Β Β Β Β β Β β Β βββ π Feature/
Β Β Β Β β Β β Β βββ π Unit/
Β Β Β Β β Β β Β βββ π TestCase.php
Β Β Β Β β Β βββ π Traits/
Β Β Β Β β Β Β Β βββ π HasUserPermissions.php # Reusable traits
Β Β Β Β β Β Β Β βββ π UserActivityTrait.php
Β Β Β Β βββ π docs/
Β Β Β Β Β Β Β Β βββ π README.mdΒ Β Β Β Β Β Β Β Β # Module documentation
Β Β Β Β Β Β Β Β βββ π CHANGELOG.md Β Β Β Β Β Β Β # Version history
Managing Your Modules: Conclusion
Plug-and-play is surely a module that makes the development of an application. But in case the application is big, the management of different independent modules becomes tricky, and the controller becomes cluttered eventually.Β
The best solution to tackle this is loose coupling, also known as a modular monolithic structure. Compared to an independent structure or microservices, a modular structure is the perfect middle ground.
RCV Core package for Laravel manages your large modules that can effortlessly be implemented on your next big project.
Ultimately, the significance of the plug-and-play module system cannot be neglected.