Your first module
In this tutorial, you build a Blog module that lists posts from the database. Start from a new Laravel 13 app with the package installed as described in Installation, using the default SQLite database. Ensure composer is on your PATH, because module:make runs composer dump-autoload.
-
Create the module.
Terminal window php artisan module:make BlogThe command ends with:
INFO Module [Blog] created successfully. -
Look at what the command generated in
Modules/Blog.DirectoryModules
DirectoryBlog
Directoryapp
DirectoryHttp
DirectoryControllers
- BlogController.php
DirectoryProviders
- BlogServiceProvider.php
- EventServiceProvider.php
- RouteServiceProvider.php
Directoryconfig
- config.php
Directorydatabase
Directoryfactories/
- …
Directorymigrations/
- …
Directoryseeders
- BlogDatabaseSeeder.php
Directoryresources
Directoryassets
Directoryjs
- app.js
Directorysass
- app.scss
Directoryviews
Directorycomponents
Directorylayouts
- master.blade.php
- index.blade.php
Directoryroutes
- api.php
- web.php
Directorytests
DirectoryFeature/
- …
DirectoryUnit/
- …
- composer.json
- module.json
- package.json
- vite.config.js
The empty folders hold a
.gitkeepfile, which the tree does not show.module:makealso wrotemodules_statuses.jsonin the app root. The module is enabled. Check it:Terminal window php artisan module:listStatus / Name ........................................ Path / priority[Enabled] Blog ........................... /path/to/app/Modules/Blog [0] -
Open
Modules/Blog/routes/web.php. It holds one resource route for the generated controller, with no middleware:<?phpuse Illuminate\Support\Facades\Route;use Modules\Blog\Http\Controllers\BlogController;Route::resource('blogs', BlogController::class)->names('blog');List the index route.
--except-path=apileaves out the API routes of the module, which are underapi/:Terminal window php artisan route:list --name=blog.index --except-path=apiGET|HEAD blogs ........ blog.index › Modules\Blog\Http\Controllers\BlogController@indexShowing [1] routes -
Start the server and open
http://127.0.0.1:8000/blogs.Terminal window php artisan serveThe page shows the heading “Hello World” and the line “Module: Blog”. Leave the server running and open a second terminal for the next steps.
-
Create the migration for a
poststable.Terminal window php artisan module:make-migration create_posts_table Blog --fields="title:string,body:text"The command creates
Modules/Blog/database/migrations/<timestamp>_create_posts_table.php. -
Create the
Postmodel.Terminal window php artisan module:make-model Post Blog --fillable=title,bodyThe command creates
Modules/Blog/app/Models/Post.php. -
Run the migration of the module.
Terminal window php artisan module:migrate BlogRunning Migration Blog Module ...........................................INFO Running migrations.<timestamp>_create_posts_table ................................ <time> DONE -
Open
Modules/Blog/database/seeders/BlogDatabaseSeeder.phpand replace its content:<?phpnamespace Modules\Blog\Database\Seeders;use Illuminate\Database\Seeder;use Modules\Blog\Models\Post;class BlogDatabaseSeeder extends Seeder{public function run(): void{Post::create(['title' => 'Hello, modules', 'body' => 'This post lives in the Blog module.']);Post::create(['title' => 'Second post', 'body' => 'Both posts come from the Post model.']);}}Run it once: a second run adds two more posts.
Terminal window php artisan module:seed BlogINFO Seeding module ...Seeding Blog Module ...........................................INFO Seeding database.Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............... RUNNINGModules\Blog\Database\Seeders\BlogDatabaseSeeder ............ <time> DONEINFO Module [Blog] seeded. -
Open
Modules/Blog/app/Http/Controllers/BlogController.phpand replace its content:<?phpnamespace Modules\Blog\Http\Controllers;use App\Http\Controllers\Controller;use Modules\Blog\Models\Post;class BlogController extends Controller{public function index(){return view('blog::index', ['posts' => Post::orderBy('id')->get()]);}} -
Open
Modules/Blog/resources/views/index.blade.phpand replace its content:<x-blog::layouts.master><h1>Posts</h1>@foreach ($posts as $post)<article><h2>{{ $post->title }}</h2><p>{{ $post->body }}</p></article>@endforeach</x-blog::layouts.master>Reload
http://127.0.0.1:8000/blogs. The page shows the heading “Posts” and two posts: “Hello, modules” and “Second post”. -
Create a feature test.
Terminal window php artisan module:make-test PostIndexTest Blog --featureThe command creates
Modules/Blog/tests/Feature/PostIndexTest.php. -
Open the test file and replace its content:
<?phpnamespace Modules\Blog\Tests\Feature;use Illuminate\Foundation\Testing\RefreshDatabase;use Modules\Blog\Models\Post;use Tests\TestCase;class PostIndexTest extends TestCase{use RefreshDatabase;public function test_the_index_lists_posts(): void{Post::create(['title' => 'Written in a test', 'body' => 'Test body.']);$this->get('/blogs')->assertOk()->assertSee('Written in a test');}}Run it:
Terminal window php artisan test Modules/Blog/tests/Feature/PostIndexTest.phpPASS Modules\Blog\Tests\Feature\PostIndexTest✓ the index lists postsTests: 1 passed (2 assertions)Duration: <time> -
Disable the module.
Terminal window php artisan module:disable BlogINFO Disabling module ...Disabling Blog Module, old status: Enabled ................... <time> DONEList the route again:
Terminal window php artisan route:list --name=blog.index --except-path=apiERROR Your application doesn't have any routes matching the given criteria.Reload
http://127.0.0.1:8000/blogs. The page shows a 404 error. -
Enable the module.
Terminal window php artisan module:enable BlogINFO Enabling module ...Enabling Blog Module, old status: Disabled ................... <time> DONETerminal window php artisan route:list --name=blog.index --except-path=apiThe route
blog.indexis back, andhttp://127.0.0.1:8000/blogsshows the two posts again.
You now have a module that owns its route, controller, model, migration, seeder, view, and test. You can switch it off and on.
To go further:
- Add routes to a module, Add Eloquent models to a module and Run the tests of a module cover the parts you used here in more detail.
- How modules boot explains what happened when you enabled and disabled the module.
- Artisan commands lists the other generators, and module.json describes the manifest.