how to custom default generate file and folder in module?
because i think some file and folder not necessary,so i want to remove it when i make a new module.
I have tried to change config in vendor/nwidart/laravel-module/config/config.php, and published by command "php artisan vendor:publish --provider="NwidartModulesLaravelModulesServiceProvider""
but nothing happens
Thanks!
As a general rule, you should avoid changing things in the vendor folder, since your changes will be overwritten whenever you run composer update. That said, I just got this working today, so it's pretty fresh in my mind, and I'll try to share what worked for me.
I found the documentation to be slightly misleading at the beginning. It claims that...
The package will automatically register a service provider and alias.
...but I found that I had to do this manually. I think this is because I'm using Laravel 5.4, not 5.5.
First, go to config/app.php. Find the providers and aliases settings towards the bottom of the file, and add the following lines to them, respectively:
'providers' => [
// Add this after all of your other providers...
Nwidart\Modules\LaravelModulesServiceProvider::class,
],
'aliases' => [
// Add this after all of your other aliases...
'Module' => Nwidart\Modules\Facades\Module::class,
],
Now, you should be able to run the command you tried before:
php artisan vendor:publish --provider="Nwidart\Modules\LaravelModulesServiceProvider"
This will create the file config/modules.php. Its primarily purpose is to tweak module generation. Read the comments in that doc, they should be helpful for answering your question.
In particular, take a look at stubs.files. You can comment out any of the lines in that array, and those files will no longer be generated. For example:
'stubs' => [
'files' => [
// Start simply registers the routes...
'start' => 'start.php',
'routes' => 'Http/routes.php',
// I don't want views in my modules, so I disabled them by default
// 'views/index' => 'Resources/views/index.blade.php',
// 'views/master' => 'Resources/views/layouts/master.blade.php',
// Unfortunately, config currently gets generated even if this is disabled
'scaffold/config' => 'Config/config.php',
'composer' => 'composer.json',
],
]
Additionally, scroll down a bit to the paths.generator section. You should see a list of directories here. If you set any of them to false (with a couple exceptions), those directories will not be generated. Here is my config, for comparison:
'generator' => [
'assets' => false, // 'Assets',
// Unfortunately config gets generated even if this is disabled
'config' => 'Config',
'command' => 'Console',
'event' => false, // 'Events',
'listener' => false, // 'Listeners',
'migration' => 'Database/Migrations',
'model' => false, // 'Entities',
'repository' => false, // 'Repositories',
// TODO: Upgrade to newest version of laravel-modules and use factories
'seeder' => 'Database/Seeders', // 'Database/Seeders',
// Currently, one stub controller will be generated even if this is set to false
'controller' => 'Http/Controllers',
'filter' => false, // 'Http/Middleware',
'request' => false, // 'Http/Requests',
// There's a minor bug w/ Provider namespaces, so avoid disabling this until its fixed
'provider' => 'Providers',
'lang' => false, // 'Resources/lang',
'views' => false, // 'Resources/views',
'policies' => false,
'rules' => false,
'test' => 'Tests',
'jobs' => false, // 'Jobs',
'emails' => false, // 'Emails',
'notifications' => false, // 'Notifications',
'resource' => false,
],
],
In my case, all that's being generated directory-wise is config, console commands, migrations, seeders, controllers, providers, and tests. Your needs may vary.
Lastly, check the namespace and paths.modules settings in that file. Both should be equal to Modules. You can play around with that after you get more acquainted with the system (I'm thinking of replacing the app directory with apps), but for now open up the composer.json in your project root and add the modules folder to the autoload.psr-4 section:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Modules\\": "modules/"
}
},
Finally, run composer dump-autoload. I think you'll need to do so every time you create a new module. Try creating a new module next:
php artisan module:make Foobar
You should have a new module created under modules/Foobar with fewer files and folders. You'll likely want to dig around FoobarServiceProvider.php and ensure that everything looks kosher. I can't give much direction in that regard, since it's both project-specific and requires general PHP + Laravel knowledge. For my app, I removed the config, view, and translation registrations. Good luck!
hi,
i have one more question. By default, model not generate after make new module, so what may i do?
thanks for your quickly answer!
Sure thing! I think you can just run the module:make-model command:
php artisan module:make-model MyModel MyModule
The model will be placed in the path specified by the paths.generator.model setting in your config/modules.php. If you changed that setting, you might need to edit the generated model and change its namespace to match its location, following PSR-4.
Check the documentation for a full list of available Artisan commands.
hi,
by the way, I have found the solution, I changed stubs file below:
`'stubs' => [
'enabled' => false,
'path' => base_path() . '/vendor/nwidart/laravel-modules/src/Commands/stubs',
'files' => [
'start' => 'start.php',
'routes' => 'Http/routes.php',
'views/index' => 'Resources/views/index.blade.php',
'views/master' => 'Resources/views/layouts/master.blade.php',
'scaffold/config' => 'Config/config.php',
'composer' => 'composer.json',
'model2' => 'Entities/model.php'
],
'replacements' => [
'start' => ['LOWER_NAME'],
'routes' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'],
'json' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'],
'views/index' => ['LOWER_NAME'],
'views/master' => ['STUDLY_NAME'],
'scaffold/config' => ['STUDLY_NAME'],
'composer' => [
'LOWER_NAME',
'STUDLY_NAME',
'VENDOR',
'AUTHOR_NAME',
'AUTHOR_EMAIL',
'MODULE_NAMESPACE',
],
'model2' => ['LOWER_NAME', 'STUDLY_NAME', 'MODULE_NAMESPACE'],
],
],`
So, when I run module-make module command, my model will auto generate.
Awesome 馃憤
@nWidart Hi I wanted to add an event in every module that is created
I changed the path in config to a stub folder in resource which I copied from vendor
but it still loads the stubs from vendor and give me error for my stub files that it does not exist