|
| 1 | +# Module loader inside your Laravel app |
| 2 | + |
| 3 | +The `zonneplan/laravel-module-loader` package provides an easy to use module loader |
| 4 | +which can be used to modulize your project. |
| 5 | + |
| 6 | +## How to use |
| 7 | + |
| 8 | +First install the package, see the installation section. |
| 9 | + |
| 10 | +#### Creating a new module: |
| 11 | + |
| 12 | +- Create a folder, for example: `Modules` in the app directory. |
| 13 | +- After that create another one, for example: `User`. |
| 14 | +- In the root of that folder insert a `UserServiceProvider` which extends our abstract `Module` class. |
| 15 | +- Implement the function `getModuleNamespace()` like: |
| 16 | +``` php |
| 17 | +namespace Modules\User; |
| 18 | + |
| 19 | +use Zonneplan/ModuleLoader/Module; |
| 20 | + |
| 21 | +class UserServiceProvider extends Module |
| 22 | +{ |
| 23 | + public function getModuleNamespace(): string |
| 24 | + { |
| 25 | + return 'user'; |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | +- Register the `UserServiceProvider` in the `config/app.php` file. |
| 30 | +``` php |
| 31 | +'providers' => [ |
| 32 | + Modules\User\UserServiceProvider::class |
| 33 | +] |
| 34 | +``` |
| 35 | + |
| 36 | +#### Structure within the module: |
| 37 | +The expected structure is seen below. Most of it is optional. |
| 38 | + |
| 39 | +```$xslt |
| 40 | +app |
| 41 | +├── Modules |
| 42 | + ├──MyModule |
| 43 | + ├──Config |
| 44 | + ├──Console |
| 45 | + ├──Database |
| 46 | + ├──Factories |
| 47 | + ├──Migrations |
| 48 | + ├──Exceptions |
| 49 | + ├──Http |
| 50 | + ├──Controllers |
| 51 | + ├──Middleware |
| 52 | + ├──Requests |
| 53 | + ├──Resources |
| 54 | + ├──Resources |
| 55 | + ├──lang |
| 56 | + ├──views |
| 57 | + ├──Routes |
| 58 | + ├──web.php |
| 59 | + ├──api.php |
| 60 | + ├──channels.php |
| 61 | + ├──console.php |
| 62 | + ├──MyModuleServiceProvider.php |
| 63 | + ├──tests |
| 64 | +``` |
| 65 | + |
| 66 | +#### Access a view from the module: |
| 67 | +To access a view from a module it will look like `'my-module::path.to.view'` |
| 68 | + |
| 69 | +For example: |
| 70 | +``` php |
| 71 | +// In a controller |
| 72 | +view('user::index'); |
| 73 | + |
| 74 | +// In a blade file |
| 75 | +@include('user::index'); |
| 76 | +@include('user::partials.form'); |
| 77 | +```` |
| 78 | + |
| 79 | + #### Registering Policies: |
| 80 | + To register policies overwrite the `$policies` variable in the ServiceProvider of your module |
| 81 | + |
| 82 | + For example: |
| 83 | +``` php |
| 84 | +protected $policies = [ |
| 85 | + MyModel::class => MyModelPolicy::class, |
| 86 | +]; |
| 87 | +``` |
| 88 | + |
| 89 | +#### Registering Middleware: |
| 90 | + To register middleware overwrite the `$middleware` variable in the ServiceProvider of your module |
| 91 | + |
| 92 | + For example: |
| 93 | +``` php |
| 94 | +protected $middleware = [ |
| 95 | + 'my-middleware' => MyMiddleware::class, |
| 96 | +]; |
| 97 | +``` |
| 98 | + |
| 99 | +#### Registering Events & Listeners: |
| 100 | + To register events & listeners overwrite the `$listen` variable in the ServiceProvider of your module |
| 101 | + |
| 102 | + For example: |
| 103 | +``` php |
| 104 | +protected $listen = [ |
| 105 | + MyEvent::class => [ |
| 106 | + MyListener::class |
| 107 | + ], |
| 108 | +]; |
| 109 | +``` |
| 110 | + |
| 111 | +#### Registering Event Subscribers: |
| 112 | + To register event subscribers overwrite the `$subscribe` variable in the ServiceProvider of your module |
| 113 | + |
| 114 | + For example: |
| 115 | +``` php |
| 116 | +protected $subscribe = [ |
| 117 | + MySubscriber::class |
| 118 | +]; |
| 119 | +``` |
| 120 | + |
| 121 | +## Requirements |
| 122 | + |
| 123 | +This package requires at least Laravel 5.8 or higher, PHP 7.2 or higher |
| 124 | + |
| 125 | +## Installation |
| 126 | + |
| 127 | +`composer require zonneplan/laravel-module-loader` |
| 128 | + |
| 129 | +The package will automatically register itself. |
| 130 | + |
| 131 | +Register the namespace: `"Modules\\": "app/Modules"` in `composer.json` like: |
| 132 | +```$xslt |
| 133 | + "autoload": { |
| 134 | + "psr-4": { |
| 135 | + "App\\": "app/", |
| 136 | + "Modules\\": "app/Modules" |
| 137 | + }, |
| 138 | + ... |
| 139 | +``` |
| 140 | + |
| 141 | +In the following files: `web.php`, `api.php`, `channels.php`, `console.php` insert respectively: |
| 142 | +``` php |
| 143 | +Route::modules('web'); |
| 144 | +Route::modules('api'); |
| 145 | +Route::modules('channels'); |
| 146 | +Route::modules('console'); |
| 147 | +``` |
| 148 | + |
| 149 | +Routes will then be searched in the `Routes` folder of every module, but don't necessarily have to exist: |
| 150 | + |
| 151 | +```$xslt |
| 152 | +├──MyModule |
| 153 | + ├──Routes |
| 154 | + ├──web.php (optional) |
| 155 | + ├──api.php (optional) |
| 156 | + ├──channels.php (optional) |
| 157 | + ├──console.php (optional) |
| 158 | +``` |
| 159 | + |
| 160 | +There is also the option to insert: |
| 161 | +``` php |
| 162 | +Route::modules(); |
| 163 | +``` |
| 164 | +Routes will then be searched in `routes.php` at the root of the module |
| 165 | + |
| 166 | +## Authors |
| 167 | + |
| 168 | +* **Aron Rotteveel** |
| 169 | +* **Dennis Stolmeijer** |
| 170 | +* **Wout Hoeve** |
| 171 | +* **Johnny Borg** |
| 172 | +* **Rick Gout** |
| 173 | +* **Thijs Nijholt** |
0 commit comments