Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DeadCode's avatar

Multiple Modules in Laravel 5

Hello! I hope you're all well!

I've been searching for quite a while on how to do a modular application in Laravel.

I've been seeing a ton of threads on L4, but none on L5 (perhaps L5 is too recent?)

Here is a directory structure I thought of:

/modules
    /Rest
        /Commands
        /Console
        /Events
        /Exceptions
        /Foundation
        /Handlers
        /Http
        /Providers
        /Services
        /Support
    /Frontend
        [Same structure]
    /Backend
        [Same Structure]
    /System
        [Same Structure]

(Is that even a good structure to use?)

Front, Backend and Rest all need some classes from /System, as they all have access to the same data. AKA Frontend to read, Backend to write, and REST for both (for access in mobile applications or with Angular and such).

Each module has it's own routes.php file to designate routes for his module.

Here are my questions:

  1. Is that a good directory structure? If not, what is?
  2. How can I include all routes.php files to make sure routing is done?

Thanks alot!

0 likes
3 replies
willvincent's avatar
Level 54

I would say if that structure works for you, then it's good.

As for question #2, adding items to the map method of RouteServiceProvider would get the job done.

Hint: app/Providers/RouteServiceProvider.php

3 likes
bobbybouwmann's avatar

That's the beautiful thing about Laravel 5, you can have your own structure however you like it ;)

2 likes
jianfengye110's avatar

Maybe you need one Model in frontend and backend, and I choose the structure like:

/app
    /Commands
    /Console
    /Http
        /Controllers
            /Backend
            /Frontend
        /Middleware
            /Backend
            /Frontend
        /Request
            /Backend
            /Frontend
        routes.php
        backend_routes.php
        frontend_routes.php
    Model.php
    ModelA.php

and My routes like that:

<?php
Route::group(array('domain' => env("FRONTEND_DOMAIN"), 'namespace' => 'Frontend'), function()
{
    Route::get('/', ['as' => 'frontend.home', 'uses' => 'IndexController@home']);
    Route::get('/info/{id}', ['as' => 'info.detail', 'uses' => 'InfoController@getDetail']);
    Route::get('/info/list/{type}', ['as' => 'info.list', 'uses' => 'InfoController@getList']);
});

require __DIR__.'/backend_routes.php';
1 like

Please or to participate in this conversation.