Laravel 5.7 Modular Structure Application Example

By Hardik Savani November 5, 2023 Category : Laravel

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...

Shares