Skip to content

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.

  1. Create the module.

    Terminal window
    php artisan module:make Blog

    The command ends with:

    INFO Module [Blog] created successfully.
  2. 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 .gitkeep file, which the tree does not show. module:make also wrote modules_statuses.json in the app root. The module is enabled. Check it:

    Terminal window
    php artisan module:list
    Status / Name ........................................ Path / priority
    [Enabled] Blog ........................... /path/to/app/Modules/Blog [0]
  3. Open Modules/Blog/routes/web.php. It holds one resource route for the generated controller, with no middleware:

    <?php
    use Illuminate\Support\Facades\Route;
    use Modules\Blog\Http\Controllers\BlogController;
    Route::resource('blogs', BlogController::class)->names('blog');

    List the index route. --except-path=api leaves out the API routes of the module, which are under api/:

    Terminal window
    php artisan route:list --name=blog.index --except-path=api
    GET|HEAD blogs ........ blog.index › Modules\Blog\Http\Controllers\BlogController@index
    Showing [1] routes
  4. Start the server and open http://127.0.0.1:8000/blogs.

    Terminal window
    php artisan serve

    The page shows the heading “Hello World” and the line “Module: Blog”. Leave the server running and open a second terminal for the next steps.

  5. Create the migration for a posts table.

    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.

  6. Create the Post model.

    Terminal window
    php artisan module:make-model Post Blog --fillable=title,body

    The command creates Modules/Blog/app/Models/Post.php.

  7. Run the migration of the module.

    Terminal window
    php artisan module:migrate Blog
    Running Migration Blog Module ...........................................
    INFO Running migrations.
    <timestamp>_create_posts_table ................................ <time> DONE
  8. Open Modules/Blog/database/seeders/BlogDatabaseSeeder.php and replace its content:

    <?php
    namespace 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 Blog
    INFO Seeding module ...
    Seeding Blog Module ...........................................
    INFO Seeding database.
    Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............... RUNNING
    Modules\Blog\Database\Seeders\BlogDatabaseSeeder ............ <time> DONE
    INFO Module [Blog] seeded.
  9. Open Modules/Blog/app/Http/Controllers/BlogController.php and replace its content:

    <?php
    namespace 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()]);
    }
    }
  10. Open Modules/Blog/resources/views/index.blade.php and 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”.

  11. Create a feature test.

    Terminal window
    php artisan module:make-test PostIndexTest Blog --feature

    The command creates Modules/Blog/tests/Feature/PostIndexTest.php.

  12. Open the test file and replace its content:

    <?php
    namespace 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.php
    PASS Modules\Blog\Tests\Feature\PostIndexTest
    ✓ the index lists posts
    Tests: 1 passed (2 assertions)
    Duration: <time>
  13. Disable the module.

    Terminal window
    php artisan module:disable Blog
    INFO Disabling module ...
    Disabling Blog Module, old status: Enabled ................... <time> DONE

    List the route again:

    Terminal window
    php artisan route:list --name=blog.index --except-path=api
    ERROR 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.

  14. Enable the module.

    Terminal window
    php artisan module:enable Blog
    INFO Enabling module ...
    Enabling Blog Module, old status: Disabled ................... <time> DONE
    Terminal window
    php artisan route:list --name=blog.index --except-path=api

    The route blog.index is back, and http://127.0.0.1:8000/blogs shows 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: