DWQA QuestionsCategory: LaravelHow to create modular based application in laravel ?
asked 6 years ago

Solutions :

Step1:

Create Folder “Modules” inside “app/”
Step2:

Module1( suppose admin )
-Controllers
-Views
-Models
-routes.php
Module2( suppose API )
-Controllers
-Views
-Models
-routes.php

Similarly you can create multiple module
Step3 : Create ModulesServiceProvider.php inside “Modules/” Folder

Step4 : Paste following code inside ModulesServiceProvider.php

<?php

namespace App\Modules;

/**
* ServiceProvider
*
* The service provider for the modules. After being registered
* it will make sure that each of the modules are properly loaded
* i.e. with their routes, views etc.
*
* @author kundan Roy <[email protected]>
* @package App\Modules
*/

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class ModulesServiceProvider extends ServiceProvider
{

/**
* Will make sure that the required modules have been fully loaded
* @return void routeModule
*/
public function boot()
{
// For each of the registered modules, include their routes and Views
$modules = config(“module.modules”);

while (list(,$module) = each($modules)) {

// Load the routes for each of the modules

if(file_exists(__DIR__.’/’.$module.’/routes.php’)) {

include __DIR__.’/’.$module.’/routes.php’;
}

if(is_dir(__DIR__.’/’.$module.’/Views’)) {
$this->loadViewsFrom(__DIR__.’/’.$module.’/Views’, $module);
}
}
}

public function register() {}

}

Step5 : Add following line inside ‘config/app.php’ file

App\Modules\ModulesServiceProvider::class,

Step6 : Create module.php file inside ‘config’ folder

Step7 : Add following code inside module.php (path => “config/module.php”)

<?php

return  [
‘modules’ => [
‘admin’,
‘web’,
‘api’
] ];

Note : You can add you module name whichever you create. here three modules.

Step8 : Run this command

composer dump-autoload

Now it will work 🙂
Thanks

for further query mail us : [email protected]