PHPackages                             erlandmuchasaj/laravel-modules - 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. erlandmuchasaj/laravel-modules

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

erlandmuchasaj/laravel-modules
==============================

Laravel modules management.

1.2.6(2mo ago)85141MITPHPPHP ^8.0|^8.1|^8.2|^8.4

Since Mar 15Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/erlandmuchasaj/laravel-modules)[ Packagist](https://packagist.org/packages/erlandmuchasaj/laravel-modules)[ Docs](https://github.com/erlandmuchasaj/laravel-modules)[ Fund](https://github.com/sponsors/erlandmuchasaj)[ Fund](https://ko-fi.com/erlandmuchasaj)[ RSS](/packages/erlandmuchasaj-laravel-modules/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (4)Versions (24)Used By (0)

Laravel modules
===============

[](#laravel-modules)

A very simple autowired laravel modules generator using laravel best practices and folder structure. If you like laravel, you will love **laravel modules** and feel just like home.

It follows the same folder structure as a Laravel project, the only difference is instead of **app/**, you have **src/**, the rest is same.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require erlandmuchasaj/laravel-modules
```

Usage
-----

[](#usage)

Basically this is all you need to do is generate a new module.

```
php artisan module:make
```

When you generate a new module, it will auto register himself automatically by adding the module to the required list in `composer.json` file

```
    "require": {
        "modules/{{module_name}}": "^1.0"
    },
```

And also it will add automatically repositories

```
    "repositories": [
        {
            "type": "path",
            "url": "./modules/*"
        }
    ]
```

and it will run `composer update` in the background to discover the newly generated module, and you are good to go.

Creating new module
-------------------

[](#creating-new-module)

Here everything is done inside a Module. Basically one module is a Laravel within another Laravel application with all its components, views, routes, middleware, events, database, seeders, factories and much, much more.

```
php artisan module:make
```

- The module is *auto-discovered* so there is no need to add it to *app/config.php* providers list.
- Then run `composer update` and you are good to go (*even this is run automatically when you add module `via php artisan module:make `*).

Artisan commands
----------------

[](#artisan-commands)

```
php artisan module:list               Show list of all modules created.
php artisan module:make               Create blueprint for a new module
php artisan module:make-cast          Create a new custom Eloquent cast class
php artisan module:make-channel       Create a new channel class
php artisan module:make-command       Create a new Artisan command
php artisan module:make-component     Create a new component-class for the specified module.
php artisan module:make-controller    Create a new controller class
php artisan module:make-event         Create a new event class
php artisan module:make-exception     Create a new custom exception class
php artisan module:make-factory       Create a new model factory
php artisan module:make-job           Create a new job class
php artisan module:make-listener      Create a new event listener class
php artisan module:make-mail          Create a new email class
php artisan module:make-middleware    Create a new middleware class
php artisan module:make-migration     Create a new migration file for module.
php artisan module:make-model         Create a new Eloquent model class
php artisan module:make-notification  Create a new notification class
php artisan module:make-observer      Create a new observer class
php artisan module:make-policy        Create a new policy class
php artisan module:make-provider      Create a new service provider class
php artisan module:make-request       Create a new form request class
php artisan module:make-resource      Create a new resource
php artisan module:make-rule          Create a new validation rule
php artisan module:make-scope         Create a new scope class
php artisan module:make-seeder        Create a new seeder class
php artisan module:make-test          Create a new test class
php artisan module:make-trait         Make trait
```

If you want to register *Events*, *Policies*, *Observers*, *Middleware*, *Commands*, *Providers* etc. you can do so on `./modules/ModuleName/Providers/EventServiceProvider` and `./modules/ModuleName/Providers/AppServiceProvider`

### Examples:

[](#examples)

Registering new provider. Go to `./modules/ModuleName/Providers/AppServiceProvider` and add the provider to `$providers` array as follows:

```
    /**
     * Get the services provided by the provider.
     *
     * @var array
     */
    protected array $providers = [
        ...
        NewServiceProvider::class, //  [
            SendEmailVerificationNotification::class,
        ],
    ];
```

Adding new policy. Go to `./modules/ModuleName/Providers/AppServiceProvider` and add to `$policies` array the `Model`and corresponding `Policy` as follows:

```
    /**
     * The policy mappings for the application.
     * Announcement::class is the model  AnnouncementPolicy::class,
    ];
```

Adding new observer. Go to `./modules/ModuleName/Providers/AppServiceProvider` and add to `$observers` array the `Model` and the corresponding `Observer` as follows:

```
    /**
     * The policy mappings for the application.
     * Announcement::class is the model  AnnouncementObserver::class,
    ];
```

Adding new command. Go to `./modules/ModuleName/Providers/AppServiceProvider` and add to `$commands` array the name of the command as follows:

```
    /**
     * The available command shortname.
     *
     * @var array
     */
    protected array $commands = [
        AppVersion::class,
        SendAnnouncementNotifications::class
    ];
```

Middleware. There are 3 types of middleware that you can add. Global, Grouped and route.

```
    /**
     * The application's global middleware stack.
     *
     * @var array
     */
    protected array $middleware = [
        ...
        AddXHeader::class, //  IpWhitelist::class, //
