Laravel 5.7 Modular Structure Application Example
In this tutorial, i would like to show you how to create modular system for your laravel 5.7 application. i will give you suggestion to write code with modular structure design pattern using nWidart/laravel-modules package.
you can quickly create module using command that setup by laravel-modules package. so basically it will create separate folder for each module.
When i had started working with laravel and i see the structure of laravel 5.7 like controller, views, model, migration, console, providers and helpers etc. I was impressed and i liked more. But still i was little bit confuse, thinking we can make it modular then it become more useful and understandable. If you create module for every crud enter level like country, state, city, item, product etc. It could be fantastic because it can re-use in your other laravel application easily. Modular structure approach is good because it more useful for us.
In this post, we will create Modular structure using laravel-modules composer package. There are several other packages for create module in laravel application. laravel-modules package create "Modules" directory and sub directory module wise. it is very simple and understandable. There are listed things on that folder:
1)Config
2)Console
3)Database
4)Entites
5)Http
6)Providers
7)Routes
8)Tests
If you want to make modular your application then follow this tutorial, So first we will install laravel-modules composer package and service provider. So run bellow command for install laravel-modules package.
Setup:
Install laravel-modules Package:
composer require nwidart/laravel-modules
Publish configuration file:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
Auto load file with Adding: conposer.json
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/"
}
}
}
Then run following command:
composer dump-autoload
Ok, now we are ready to create module by using laravel-modules package command, So you can simply create new package as like bellow syntax:
Syntax for create module:
php artisan make:module module_name
Example for create module:
php artisan make:module Category
After run above command, you can see new migration and Modules directory on app folder. structure looks like as bellow:
app/
bootstrap/
vendor/
Modules/
├── Category/
├── Assets/
├── Config/
├── Console/
├── Database/
├── Migrations/
├── Seeders/
├── Entities/
├── Http/
├── Controllers/
├── Middleware/
├── Requests/
├── Providers/
├── CategoryServiceProvider.php
├── RouteServiceProvider.php
├── Resources/
├── assets/
├── js/
├── app.js
├── sass/
├── app.scss
├── lang/
├── views/
├── Routes/
├── api.php
├── web.php
├── Repositories/
├── Tests/
├── composer.json
├── module.json
├── package.json
├── webpack.mix.js
As you can see there are controllers, models, views, database, repository, routes and helper. So you can simply create more module as you require. You can simply run by following command:
php artisan serve
Open in your browser:
http://localhost:8000/category
You can get more information from here: Click Here
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Integrate Stripe Payment Gateway in Laravel?
- Laravel 5.7 Import Export Excel to database Example
- How to use Google ReCaptcha in Laravel?
- Implement Flash Message with Laravel 5.7
- Laravel 5.7 - Create REST API with authentication using Passport Tutorial
- How to send mail using queue in Laravel 5.7?
- Laravel 5.7 JQuery Form Validation Example
- Laravel 5.7 - Pagination Link Customizations Example
- Laravel 5.7 Autocomplete Search from Database using Typeahead JS
- Laravel 5.7 Ajax Pagination Example