PHPackages                             sebastiaanluca/laravel-module-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sebastiaanluca/laravel-module-loader

Abandoned → [nwidart/laravel-modules](/?search=nwidart%2Flaravel-modules)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

sebastiaanluca/laravel-module-loader
====================================

A lightweight package to split your code into individual modules.

7.0.0(5y ago)754.3k71MITPHPPHP ^8.0

Since Jul 25Pushed 5y ago1 watchersCompare

[ Source](https://github.com/sebastiaanluca/laravel-module-loader)[ Packagist](https://packagist.org/packages/sebastiaanluca/laravel-module-loader)[ Docs](https://github.com/sebastiaanluca/laravel-module-loader)[ RSS](/packages/sebastiaanluca-laravel-module-loader/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (4)Versions (31)Used By (1)

A lightweight package to split your code into contextual modules
================================================================

[](#a-lightweight-package-to-split-your-code-into-contextual-modules)

[![Latest stable release](https://camo.githubusercontent.com/5a8869c4282eda224ccdc5e63e8d2337314ff67518fa30a40f9b8a26c54ab704/68747470733a2f2f706f7365722e707567782e6f72672f7365626173746961616e6c7563612f6c61726176656c2d6d6f64756c652d6c6f616465722f76657273696f6e)](https://packagist.org/packages/sebastiaanluca/laravel-module-loader)[![Software license](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)[![Build status](https://github.com/sebastiaanluca/laravel-module-loader/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/sebastiaanluca/laravel-module-loader/actions/workflows/test.yml?query=branch%3Amaster)[![Total downloads](https://camo.githubusercontent.com/7af3a6e1f75bc8ae463cb67b02d95fd8cb30de70116c6cb790382ee702520ff9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7365626173746961616e6c7563612f6c61726176656c2d6d6f64756c652d6c6f616465722e737667)](https://packagist.org/packages/sebastiaanluca/laravel-module-loader)

[![Read my blog](https://camo.githubusercontent.com/0760b582e22202bd412feb64f5f87b6459f64a0b8ea5fdd24cbadf87df736177/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d626c6f672d6c69676874677265792e737667)](https://sebastiaanluca.com/blog)[![View my other packages and projects](https://camo.githubusercontent.com/5ce3a83ee0ec2b4ae47a99f67347c1d8e78582757eca80fe31c98e5bd0e2185a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c696e6b2d6f746865725f7061636b616765732d6c69676874677265792e737667)](https://packagist.org/packages/sebastiaanluca)[![Follow @sebastiaanluca on Twitter](https://camo.githubusercontent.com/bde346ed8efc8b2458386b36256c64e3fe010276f449b80fe4f663c4c8bbd83e/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f7365626173746961616e6c7563612e7376673f7374796c653d736f6369616c)](https://twitter.com/sebastiaanluca)[![Share this package on Twitter](https://camo.githubusercontent.com/cb820a0ecc9645168e33b03925d7f14691262ddbaeaf66a0a91697803d0cba2d/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f75726c2f687474702f736869656c64732e696f2e7376673f7374796c653d736f6369616c)](https://twitter.com/intent/tweet?text=A%20lightweight%20Laravel%20package%20to%20split%20your%20code%20into%20individual%20modules.%20Via%20@sebastiaanluca%20https://github.com/sebastiaanluca/laravel-module-loader)

**Laravel Module Loader helps you organize your project by splitting your domain code in contextual modules.**

By default, Laravel provides you with an `app/` directory to put all your classes in. In medium to large projects, you usually end up having a monolithic `app/` directory with tens or hundreds of directories and classes. To make *some* sense of that, they're typically organized per *type of class* (e.g. providers, services, models, …) too.

**An alternative** to that is organizing your code in groups that belong in the same context, i.e. modules. Since you usually develop one feature at a time, it makes it easier for you and other developers to find related code when working in that context.

Additionally, this lightweight module package provides some added benefits when making use of the include module service provider, including:

- individual configuration;
- publishable configuration;
- migrations;
- seeders;
- Eloquent factories;
- (PHP array or JSON) translations;
- views;
- Eloquent polymorphic mapping support;
- automatic event listener or subscriber registration;
- and automatic [router mapping](https://github.com/sebastiaanluca/laravel-router).

### Example

[](#example)

To give you a sense of how a modularized project is structured, take for instance a users, documents, and a shopping cart module:

```
User/
    database/
        factories/
        migrations/
        seeds/
    resources/
        lang/
        views/
    src/
        Commands/
        Jobs/
        Models/
        Providers/
            UserServiceProvider
    tests/
        UserTestCase

Document/
    resources/
        lang/
    src/
        Models/
        Providers/
            DocumentServiceProvider

ShoppingCart/
    src/
        Providers/
            ShoppingCartServiceProvider

```

Table of contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [How to install](#how-to-install)
- [Getting started](#getting-started)
    - [Creating a module](#creating-a-module)
    - [Caching in production environments](#caching-in-production-environments)
- [Going into detail](#going-into-detail)
    - [Scanning and registering modules](#scanning-and-registering-modules)
    - [Using a module service provider](#using-a-module-service-provider)
    - [Individual module configuration](#individual-module-configuration)
        - [Publishing a module's configuration](#publishing-a-modules-configuration)
    - [Using migrations](#using-migrations)
    - [Using factories](#using-factories)
    - [Using seeders](#using-seeders)
    - [Using translations](#using-translations)
    - [Using views](#using-views)
    - [Simplified polymorphic model type mapping](#simplified-polymorphic-model-type-mapping)
    - [Simplified event listener registration](#simplified-event-listener-registration)
    - [Automatic router mapping](#automatic-router-mapping)
    - [Automatic service provider registration](#automatic-service-provider-registration)
- [Package configuration](#package-configuration)
    - [Runtime autoloading](#runtime-autoloading)
    - [Module directories](#module-directories)
    - [Development environments](#development-environments)
- [License](#license)
- [Change log](#change-log)
- [Testing](#testing)
- [Contributing](#contributing)
- [Security](#security)
- [Credits](#credits)
- [About](#about)

Requirements
------------

[](#requirements)

- PHP 8 or higher
- Laravel 8 or higher

How to install
--------------

[](#how-to-install)

Via Composer:

```
composer require sebastiaanluca/laravel-module-loader
```

Getting started
---------------

[](#getting-started)

There's only one required step to get started and that is to *create a module*.

### Creating a module

[](#creating-a-module)

Say you want to create a module to group all your *user* related classes and files, run:

```
php artisan modules:create User

```

and the package will:

- scaffold your user module directories under your primary configured module directory;
- generate a correctly named (optional) service provider to fully enable the power of this package;
- and write its autoload information to your composer.json file.

Of course you can also do all of this manually. Create a `User` and `User/src` directory in any of your module directories and run `php artisan modules:refresh` afterwards to add it to your composer.json file (or do so manually too).

### Caching in production environments

[](#caching-in-production-environments)

*Optional*

To reduce the amount of files being read during application boot, you can opt to **cache a list of your module service providers** to improve load time. In addition to Composer providing a cached list of all your module classes, the package reads just the one cache file on boot and registers those instead of scanning all your module directories and loading them on-the-fly. Especially useful in production environments and advised to run during project deployment.

To cache all providers, run:

```
php artisan modules:cache

```

To clear the cache file, execute:

```
php artisan modules:clear

```

Going into detail
-----------------

[](#going-into-detail)

### Scanning and registering modules

[](#scanning-and-registering-modules)

When you've manually created a new module, made some changes, added directories that need autoloading, and so on, you should refresh your modules:

```
php artisan modules:refresh

```

This will scan all your module directories for changes, write your modules' autoload configuration to composer.json (both classmap, PSR-4, and PSR-4 dev sections when applicable), and automatically update the Composer autoloader.

Your existing Composer configuration is not altered and no autoload entries are altered, unless:

- the module is missing;
- there are duplicate modules (by which one entry will be kept).

If you wish to keep all autoload entries for **modules that do not exist**, you can use the `--keep` option:

```
php artisan modules:refresh --keep

```

### Using a module service provider

[](#using-a-module-service-provider)

*Optional*

A module should contain a service provider if you want your module to support:

- individual configuration;
- publishable configuration;
- migrations;
- seeders;
- Eloquent factories;
- (PHP array or JSON) translations;
- views;
- Eloquent polymorphic mapping support;
- automatic event listener or subscriber registration;
- and automatic [router mapping](https://github.com/sebastiaanluca/laravel-router).

When you create a module, a service provider is generated for you. If you want to create a provider yourself and have it automatically be registered on framework boot, create a class extending the correct base provider in your module's `src/Providers` directory.

Pay extra attention to the *studly cased* naming of your provider, as it's only registered on boot if the class name starts with your module name.

```
