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

Ishit's avatar
Level 1

Modular approach in Laravel 12.03

Hello,

I want to develop on the latest Laravel product using a modular approach. I have tried many times with the "nwidart/laravel-modules" package.

I ran the command to create the Ecommerce module, and it was successfully created. However, when I run the composer dump-autoload command, I encounter the following error:

"Modules\Ecommerce\App\Providers\EcommerceServiceProvider" not found.

My EcommerceServiceProvider is located in the directory Modules\Ecommerce\app\Providers.

I have also modified the Modules\Ecommerce\module.json and added the provider to the config/app.php file.

So guide me how can i resolve this error for Laravel 12 ?

Thanks

1 like
7 replies
vincent15000's avatar

@Tray2 I never worked in a modular approach.

But I'd love creating some packages / modules that would add automatically some features to the application.

For example, instead of coding the contacts list, I just add the contacts package to the application.

And being able to activate or deactivate the functionality inside the application.

I wonder if the modular approach is the best one to do something like that.

Tray2's avatar

@vincent15000 Perhaps, never given it much thought, I just don't like deviating from the Laravel MVC.

1 like
vincent15000's avatar

@Tray2 I agree with you, if Laravel was modular, it would be architectured differently.

martinbean's avatar

@ishit I wouldn’t use a package to create modules. It’s just completely overkill and unnecessary.

If you really wanted to carve your codebase up into modules, then this is how I would personally approach it.

  1. Create a /modules directory.
  2. Create modules inside this source directory with their own service providers.
  3. Add entries to my composer.json to auto-load classes from those modules.

Classes in each sub-directory under the /modules directory could then have their own namespace, and any classes related to that “module” (such as models, controllers, jobs, console commands, etc). You’d register those modules’ assets (migrations, views, etc) using the helper methods available in the service provider class.

So, if you have a “shop” module at modules/shop then you’d have a ShopServiceProvider class that could register all of the resources for that module:

// File: modules/shop/src/ShopServiceProvider.php
namespace Vendor\Shop;

class ShopServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Register any shop-related services like payment gateways, etc here...
    }

    public function boot(): void
    {
        $this->publishesMigrations([
            __DIR__ . '/../database/migrations' => $this->app->databasePath('migrations'),
        ]);

        $this->loadRoutesFrom(__DIR__ . '/../routes/web.php');

        $this->loadViewsFrom(__DIR__ . '/../resources/views', 'shop');
    }
}

You‘d then need to add an entry to the autoload section of your application’s composer.json file to tell Laravel where to find this module’s files:

  "autoload": {
      "psr-4": {
          "App\\": "app/",
          "Database\\Factories\\": "database/factories/",
-         "Database\\Seeders\\": "database/seeders/"
+         "Database\\Seeders\\": "database/seeders/",
+         "Vendor\\Shop\\": "modules/shop/"
      }
  },

You’d also need to add your modules’ service providers to the array in your bootstrap/providers.php file:

  return [
      App\Providers\AppServiceProvider::class,
+     Vendor\Shop\ShopServiceProvider::class,
  ];
2 likes

Please or to participate in this conversation.