Courses

How to Structure Laravel 12 Projects

Modules, Packages or... Mix of Both?

Summary of this lesson:
- Using nWidart/laravel-modules for modular structure
- Creating separate packages for reusability
- Implementing internal packages as modules
- Managing module dependencies

In this lesson, we will discuss options to organize a larger project that can be divided into sub-projects. I will show you three ways.


Option 1. Divide into Modules

One approach to structure larger projects is by sub-systems or modules, like expense management module, customer management module, etc.

You can have separate developers or teams working only on specific module(s). One way to do it is by using a package called nWidart/laravel-modules.

The package can be installed via composer.

composer require nwidart/laravel-modules

Optionally, you can publish the package's configuration file by running:

php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"

Then, the Modules folder must be added to the autoload in composer.

composer.json:

{
// ...
"autoload": {
"psr-4": {
"App\\": "app/",
"Modules\\": "Modules/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
// ...
}

Don't forget to run composer dump-autoload afterward.

New modules are created using the artisan command.

php artisan module:make blog

This will create a module in the path Modules/Blog with the following structure:

Modules/
├── Blog/
├──app
├── Http/
├── Controllers/
├── Providers/
├── BlogServiceProvider.php
├── RouteServiceProvider.php
├── config/
├── database/
├── seeders/
├── resources/
├── assets/
├── views/
├── routes/
├── api.php
├── web.php
├── composer.json
├── module.json
├── package.json
├── vite.config.js

Everything related to the blog goes inside this module's folder. The views are called similar to how packages work by first calling the module name blog::.

For example, if you have index.blade.php inside the Modules/Blog/resources/views folder, you would call it using the blog::index syntax.

You can check a YouTube video example of converting simple applications to modules here: Laravel Modules Demo: Make Your Project Modular.

And you can check the difference between converted applications in the GitHub here.


Option 2. Separate into Packages

Another option to divide the functionality of a project is to create a package.

If you have more than one...

The full lesson is only for Premium Members.
Want to access all 11 lessons of this course? (56 min read)

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord