Creating Your First Module
Generate a new module
Use the This creates a complete module structure in
make:module Artisan command to scaffold a new module:app-modules/blog/.Update dependencies
The command automatically updates your
composer.json file. Run composer update to register the module:Module Structure
A freshly created module has the following structure:Key Directories
src/ - Source Code
src/ - Source Code
Contains all your PHP classes:
Controllers/- HTTP controllersModels/- Eloquent modelsView/Components/- Blade componentsProviders/- Service providersConsole/Commands/- Artisan commands
routes/ - Route Definitions
routes/ - Route Definitions
All PHP files in this directory are automatically loaded. Routes are loaded alphabetically by filename.
resources/ - Views and Assets
resources/ - Views and Assets
views/- Blade templates with automatic namespacinglang/- Translation files
database/ - Database Files
database/ - Database Files
migrations/- Database migrationsfactories/- Model factoriesseeders/- Database seeders
Module Configuration
Composer.json
Each module has its owncomposer.json file that defines its namespace and autoloading:
composer.json
The namespace prefix (
Modules) can be customized in your config/app-modules.php file.Service Provider
Every module includes a service provider that’s automatically registered:src/Providers/BlogServiceProvider.php
Namespace Configuration
By default, modules use theModules namespace. You can customize this by publishing the config:
config/app-modules.php:
config/app-modules.php
Working with Modules
List All Modules
View all registered modules:Cache Module Configuration
For better performance in production, cache your module configuration:Sync Modules
If you manually add or remove modules, sync the composer configuration:Next Steps
Now that you understand module structure, learn about specific components:Module Components
Create models, controllers, and other components
Module Routing
Define routes within your modules
Module Views
Work with Blade templates and components
Module Migrations
Manage database migrations