Laravel Modular extends all of Laravel’s make:* commands to support modules. You can create any component within a module using the --module flag.
Creating Components
All standard Laravel generator commands work with modules:
Controllers
Models
Middleware
Requests
php artisan make:controller PostController --module=blog
Available Generators
Laravel Modular supports all Laravel make commands:
make:controller - HTTP controllers
make:model - Eloquent models
make:middleware - HTTP middleware
make:request - Form requests
make:resource - API resources
make:migration - Database migrations
make:factory - Model factories
make:seeder - Database seeders
make:event - Event classes
make:listener - Event listeners
make:job - Queued jobs
make:observer - Model observers
make:component - Blade components
make:command - Artisan commands
make:notification - Notifications
make:mail - Mailable classes
make:policy - Authorization policies
make:rule - Validation rules
make:exception - Exception classes
make:cast - Custom casts
make:channel - Broadcast channels
make:test - Tests
Models
Models are created in the src/Models directory:
php artisan make:model Post --module=blog
This generates:
app-modules/blog/src/Models/Post.php
<? php
namespace Modules\Blog\Models ;
use Illuminate\Database\Eloquent\ Model ;
class Post extends Model
{
protected $table = 'posts' ;
}
Combine with other flags like --migration, --factory, --seed to generate related files: php artisan make:model Post --module=blog -mfs
Controllers
Controllers are created in the src/Controllers directory:
php artisan make:controller PostController --module=blog
Generated controller:
app-modules/blog/src/Controllers/PostController.php
<? php
namespace Modules\Blog\Controllers ;
use App\Http\Controllers\ Controller ;
use Illuminate\Http\ Request ;
class PostController extends Controller
{
//
}
Resource Controllers
Create a resource controller with all CRUD methods:
php artisan make:controller PostController --module=blog --resource
API Controllers
Create an API resource controller:
php artisan make:controller PostController --module=blog --api
Blade Components
Blade components are automatically registered and namespaced by module name.
Creating a Component
php artisan make:component Alert --module=blog
This creates two files:
app-modules/blog/src/View/Components/Alert.php
app-modules/blog/resources/views/components/alert.blade.php
<? php
namespace Modules\Blog\View\Components ;
use Illuminate\View\ Component ;
class Alert extends Component
{
public function __construct (
public string $type = 'info'
) {
}
public function render ()
{
return view ( 'blog::components.alert' );
}
}
Using Components
Components are namespaced by module name:
< x-blog::alert type = "success" >
Post created successfully!
</ x-blog::alert >
The blog:: prefix matches your module name. Components in a blog module use <x-blog::component-name>.
Nested Components
Create components in subdirectories:
php artisan make:component Cards/Featured --module=blog
Use with dot notation:
< x-blog::cards.featured />
Artisan Commands
Create custom Artisan commands within modules:
php artisan make:command PublishPost --module=blog
Generated command:
app-modules/blog/src/Console/Commands/PublishPost.php
<? php
namespace Modules\Blog\Console\Commands ;
use Illuminate\Console\ Command ;
class PublishPost extends Command
{
protected $signature = 'blog:publish {post}' ;
protected $description = 'Publish a blog post' ;
public function handle ()
{
//
}
}
Commands are automatically discovered and registered. No need to manually register them!
Events and Listeners
Create events and their listeners:
Create an event
php artisan make:event PostPublished --module=blog
Create a listener
php artisan make:listener SendPostNotification --module=blog --event=PostPublished
Auto-discovery
If you have event discovery enabled, listeners are automatically registered. Otherwise, register them in your service provider.
Policies
Create authorization policies for your models:
php artisan make:policy PostPolicy --module=blog --model=Post
Generated policy:
app-modules/blog/src/Policies/PostPolicy.php
<? php
namespace Modules\Blog\Policies ;
use Modules\Blog\Models\ Post ;
use App\Models\ User ;
class PostPolicy
{
public function view ( User $user , Post $post )
{
//
}
public function update ( User $user , Post $post )
{
//
}
}
Policies are automatically discovered and registered with the Gate.
Create form request validation classes:
php artisan make:request StorePostRequest --module=blog
Example request:
app-modules/blog/src/Requests/StorePostRequest.php
<? php
namespace Modules\Blog\Requests ;
use Illuminate\Foundation\Http\ FormRequest ;
class StorePostRequest extends FormRequest
{
public function authorize ()
{
return true ;
}
public function rules ()
{
return [
'title' => 'required|max:255' ,
'content' => 'required' ,
'published_at' => 'nullable|date' ,
];
}
}
Tests
Create tests within your module:
php artisan make:test PostControllerTest --module=blog
Tests extend your application’s base TestCase:
app-modules/blog/tests/PostControllerTest.php
<? php
namespace Modules\Blog\Tests ;
use Tests\ TestCase ;
class PostControllerTest extends TestCase
{
public function test_can_view_posts ()
{
$response = $this -> get ( '/posts' );
$response -> assertStatus ( 200 );
}
}
The base test class is configurable in config/app-modules.php via the tests_base option.
Component Namespace Resolution
All components automatically resolve to the correct module namespace:
// In a Blog module:
Modules \ Blog \ Models \ Post
Modules \ Blog \ Controllers \ PostController
Modules \ Blog \ View \ Components \ Alert
Modules \ Blog \ Policies \ PostPolicy
Make sure to run composer dump-autoload if classes are not being found after creation.
Next Steps
Module Routing Learn how to define routes in your modules
Module Views Work with Blade templates and views
Module Factories Create model factories for testing
Customizing Stubs Customize generator stub templates