Laravel 5.7 Modular Structure Application - Laravel Misc.
Laravel 5.7 introduced a new feature called "Package Discovery", which allows you to easily add and use packages in your Laravel application. With this feature, you can create a modular structure for your Laravel application that allows for better code organization and reusability. In this article, we'll explore how to build a modular structure application in Laravel 5.7.
Laravel Modular Structure
1. Creating a Module
To create a module, you need to create a directory inside the "Modules" directory that matches the name of your module. Inside this directory, you can create a structure that looks like this:
- MyModule/
- Providers/
- MyModuleServiceProvider.php
- Controllers/
- MyModuleController.php
- Routes/
- web.php
- Resources/
- views/
- index.blade.php
- lang/
- en/
- messages.php
Syntax
php artisan make:provider MyModuleServiceProvider
Example
php artisan make:provider MyModuleServiceProvider
Output
This command will create a new file "MyModuleServiceProvider.php" in the "Providers" directory inside the "MyModule" directory.
Explanation
A module is a self-contained package of code that has its own controllers, routes, views, models, and other components. By creating a modular structure, you can organize your code better and make it more reusable.
Use
Modular structure can be used in any Laravel application to organize the code and make it more reusable. It's particularly useful in large applications where the codebase can become unwieldy.
2. Registering a Module
Once you've created a module, you need to register it with the Laravel application. You can do this by adding the following code to your "config/app.php" file:
'providers' => [
/* Other Service Providers */
App\Modules\MyModule\Providers\MyModuleServiceProvider::class,
],
Syntax
App\Modules\MyModule\Providers\MyModuleServiceProvider::class,
Example
'providers' => [
/* Other Service Providers */
App\Modules\MyModule\Providers\MyModuleServiceProvider::class,
],
Output
This code will register the "MyModuleServiceProvider" with the Laravel application.
Explanation
Registering a module with the Laravel application allows you to use the components of the module in your Laravel application.
Use
You can use this feature to organize your code better and make it more modular. You can also use it to add new functionality to your Laravel application without modifying the existing code.
3. Using a Module
To use a module in your Laravel application, you need to create a route in your "web.php" file that points to the controller of the module.
Route::get('/mymodule', '[email protected]');
Syntax
Route::get('/mymodule', '[email protected]');
Example
Route::get('/mymodule', 'MyModuleController@index');
Output
This code will create a new route to the "index" method of the "MyModuleController" controller.
Explanation
This code creates a new route in your Laravel application that points to the controller of your module.
Use
You can use this feature to add new functionality to your Laravel application without modifying the existing code.
Summary
In this article, we explored how to build a modular structure application in Laravel 5.7. We discussed how to create a module, register it with your Laravel application, and use it to add new functionality to your application. Laravel's modular structure allows you to organize your code better and make it more reusable. By using this feature, you can build scalable and maintainable Laravel applications that are easy to extend and modify.