You can publish the package configuration using the following command:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
In the published configuration file you can configure the following things:
Default namespace
What the default namespace will be when generating modules.
Default: Modules
The default namespace is set as Modules this will apply the namespace for all classes the module will use when it's being created and later when generation additional classes.
Overwrite the generated files (stubs)
Overwrite the default generated stubs to be used when generating modules. This can be useful to customise the output of different files.
These stubs set options and paths.
Enabled true or false will enable or disable a module upon creation, the default is false meaning you will have to enable a module manually.
To enable a module edit module_statuses.json or run the command:
php artisan module:enable ModuleName
note the module_statues.json file will be created if it does not exist using this command.
The contents of module_statuses.json looks like:
{
"Users": true
}
The above would be when there is a single module called Users and is enabled.
Path points to a vendor directly where the default stubs are located, these can be published and modified.
Files set the file locations defaults.
Replacements is a way to do a Find and Replace on generation any matches will be replaced.
Comment out any files below that you do not want to be generated.
For example if you do not intend to use any frontend assets there is no need for CSS/JS files or asset loading files so turn them off:
'files' => [
'routes/web' => 'routes/web.php',
'routes/api' => 'routes/api.php',
'views/index' => 'resources/views/index.blade.php',
'views/master' => 'resources/views/layouts/master.blade.php',
'scaffold/config' => 'config/config.php',
'composer' => 'composer.json',
//'assets/js/app' => 'resources/js/app.js',
//'assets/sass/app' => 'resources/css/app.css',
//'vite' => 'vite.config.js',
//'package' => 'package.json',
],
The complete list:
'stubs' => [
'enabled' => false,
'path' => base_path('vendor/nwidart/laravel-modules/src/Commands/stubs'),
'files' => [
'routes/web' => 'routes/web.php',
'routes/api' => 'routes/api.php',
'views/index' => 'resources/views/index.blade.php',
'views/master' => 'resources/views/layouts/master.blade.php',
'scaffold/config' => 'config/config.php',
'composer' => 'composer.json',
'assets/js/app' => 'resources/assets/js/app.js',
'assets/sass/app' => 'resources/assets/sass/app.scss',
'vite' => 'vite.config.js',
'package' => 'package.json',
],
'replacements' => [
'routes/web' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'CONTROLLER_NAMESPACE'],
'routes/api' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'STUDLY_NAME', 'CONTROLLER_NAMESPACE'],
'vite' => ['LOWER_NAME'],
'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE', 'PROVIDER_NAMESPACE'],
'views/index' => ['LOWER_NAME'],
'views/master' => ['LOWER_NAME', 'STUDLY_NAME'],
'scaffold/config' => ['STUDLY_NAME'],
'composer' => [
'LOWER_NAME',
'STUDLY_NAME',
'VENDOR',
'AUTHOR_NAME',
'AUTHOR_EMAIL',
'MODULE_NAMESPACE',
'PROVIDER_NAMESPACE',
],
],
'gitkeep' => true,
],
Generator Path
These are the files that are generated by default where generate is set to true
, when false is used that path is not generated.
'generator' => [
// app/
'channels' => ['path' => 'app/Broadcasting', 'generate' => false],
'command' => ['path' => 'app/Console', 'generate' => false],
'emails' => ['path' => 'app/Emails', 'generate' => false],
'event' => ['path' => 'app/Events', 'generate' => false],
'jobs' => ['path' => 'app/Jobs', 'generate' => false],
'listener' => ['path' => 'app/Listeners', 'generate' => false],
'model' => ['path' => 'app/Models', 'generate' => false],
'notifications' => ['path' => 'app/Notifications', 'generate' => false],
'observer' => ['path' => 'app/Observers', 'generate' => false],
'policies' => ['path' => 'app/Policies', 'generate' => false],
'provider' => ['path' => 'app/Providers', 'generate' => true],
'repository' => ['path' => 'app/Repositories', 'generate' => false],
'resource' => ['path' => 'app/Transformers', 'generate' => false],
'rules' => ['path' => 'app/Rules', 'generate' => false],
'component-class' => ['path' => 'app/View/Components', 'generate' => false],
// app/HTTP/
'controller' => ['path' => 'app/Http/Controllers', 'generate' => true],
'filter' => ['path' => 'app/Http/Middleware', 'generate' => false],
'request' => ['path' => 'app/Http/Requests', 'generate' => false],
// config/
'config' => ['path' => 'config', 'generate' => true],
// database/
'migration' => ['path' => 'database/migrations', 'generate' => false],
'seeder' => ['path' => 'database/seeders', 'generate' => false],
'factory' => ['path' => 'database/factories', 'generate' => false],
// lang/
'lang' => ['path' => 'lang', 'generate' => false],
// resource/
'assets' => ['path' => 'resources/assets', 'generate' => false],
'views' => ['path' => 'resources/views', 'generate' => true],
'component-view' => ['path' => 'resources/views/components', 'generate' => false],
// routes/
'routes' => ['path' => 'routes', 'generate' => true],
// tests/
'test-unit' => ['path' => 'tests/Unit', 'generate' => false],
'test-feature' => ['path' => 'tests/Feature', 'generate' => false],
],
Don't like Entities for the Models here's where you can change the path to Models instead.
From v11 the config has been changed to use Models instead of Entities, you can change this in your config.
Instead of Entities:
'model' => ['path' => 'Entities', 'generate' => true],
Use of Models:
'model' => ['path' => 'Models', 'generate' => true],
Package Commands
As from v10.0.5
the commands are loaded by an internal ConsoleServiceProvider
.
The commands are autoloaded internally. You can add any custom commands by using the ->merge()
method.
'commands' => ConsoleServiceProvider::defaultCommands()
->merge([
// New commands go here
])->toArray(),
For existing configs please replace the commands with:
'commands' => ConsoleServiceProvider::defaultCommands()
->merge([
// New commands go here
])->toArray(),
Overwrite the paths
Overwrite the default paths used throughout the package.
Set the path for where to place the Modules folder, where the assets will be published and the location for the migrations.
It's recommend keep the defaults here.
'paths' => [
'modules' => base_path('Modules'),
'assets' => public_path('modules'),
'migration' => base_path('database/migrations'),
Scan additional folders for modules
By default, modules are loaded from a directory called Modules, in addition to the scan path. Any packages installed for modules can be loaded from here.
'scan' => [
'enabled' => false,
'paths' => [
base_path('vendor/*/*'),
],
],
You can add your own locations for instance say you're building a large application and want to have multiple module folder locations, you can create as many as needed.
'scan' => [
'enabled' => true,
'paths' => [
base_path('ModulesCms'),
base_path('ModulesERP'),
base_path('ModulesShop'),
],
],
Remember to set
enabled
totrue
to enable these locations.
Composer file template
When generating a module the composer.json file will contain the author details as set out below, change them as needed.
Take special notice of the vendor, if you plan on extracting modules to packages later it's recommend using your BitBucket/GitHub/GitLab vendor name here.
'composer' => [
'vendor' => 'nwidart',
'author' => [
'name' => 'Nicolas Widart',
'email' => 'n.widart@gmail.com',
],
]
Caching
from v11.1.0 The cache system has been removed the following can be removed from your config.
'cache' => [
'enabled' => false,
'driver' => 'file',
'key' => 'laravel-modules',
'lifetime' => 60,
],
Also, from `'file' section the following can be removed:
'cache-key' => 'activator.installed',
'cache-lifetime' => 604800,
To cache use php artisan module
and php artisan optimize:clear
to clear the cache.
Registering custom namespace
Decide which custom namespaces need to be registered by the package. If one is set to false, the package won't handle its registration.
Muiltiple config files
From v11.1.5
the package now supports multiple config files, this allows for a more modular approach to the configuration.
The default config file is config.php
.
This contains:
<?php
return [
'name' => 'ModuleName',
];
To use this config file you can use the following for a module called books:
{{ config('books.name') }}
An example of using multiple config files, event nested folders are supported:
config/
admin/admin.php
admin.php
config.php
user.php
Then to use them config('module.file.name')
for example config('books.service.name')
to load config/service.php
from a module called Books