Skip to content

Add routes to a module

To serve a page and a JSON endpoint from the Blog module, add routes to its generated route files. You need an enabled Blog module and two controllers (PostController and Api/PostController):

Terminal window
php artisan module:make-controller Post Blog
php artisan module:make-controller Api/Post Blog --api

See Add controllers to a module for controller options.

module:make creates both route files for a web module, and the module’s RouteServiceProvider loads them:

File Generated routes Applied by RouteServiceProvider
Modules/Blog/routes/web.php Route::resource('blogs', BlogController::class)->names('blog') web middleware group, no prefix
Modules/Blog/routes/api.php Route::apiResource('blogs', BlogController::class)->names('blog') under auth:sanctum and prefix v1 api middleware group, prefix api, name prefix api.

The generated web route requires no login. The generated API route uses auth:sanctum, so requests without Sanctum or a valid token return 401.

  1. Add a web route for PostController in Modules/Blog/routes/web.php. Keep the generated Route::resource() line if you still need the blogs routes.

    Modules/Blog/routes/web.php
    <?php
    use Illuminate\Support\Facades\Route;
    use Modules\Blog\Http\Controllers\BlogController;
    use Modules\Blog\Http\Controllers\PostController;
    Route::resource('blogs', BlogController::class)->names('blog');
    Route::get('posts', [PostController::class, 'index'])->name('blog.posts.index');

    Route names are global across the application. Prefix every name with the module name to avoid collisions.

  2. Replace Modules/Blog/routes/api.php with a public API route without auth:sanctum. RouteServiceProvider already applies the api URL prefix and api. name prefix.

    Modules/Blog/routes/api.php
    <?php
    use Illuminate\Support\Facades\Route;
    use Modules\Blog\Http\Controllers\Api\PostController;
    Route::prefix('v1')->group(function () {
    Route::get('posts', [PostController::class, 'index'])->name('blog.posts.index');
    });

    This serves /api/v1/posts under the name api.blog.posts.index.

  3. Check the result.

    Terminal window
    php artisan route:list --path=posts
    GET|HEAD api/v1/posts ......... api.blog.posts.index › Modules\Blog\Http\Controllers\Api\PostController@index
    GET|HEAD posts ................ blog.posts.index › Modules\Blog\Http\Controllers\PostController@index

Wrap module routes in prefix() and name() to group them under a shared path and name prefix:

Modules/Blog/routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use Modules\Blog\Http\Controllers\PostController;
Route::prefix('blog')->name('blog.')->group(function () {
Route::get('posts', [PostController::class, 'index'])->name('posts.index');
Route::get('posts/{post}', [PostController::class, 'show'])->name('posts.show');
});

This creates /blog/posts (blog.posts.index) and /blog/posts/{post} (blog.posts.show).

The generated show() method returns view('blog::show'). Create that view so /blog/posts/{post} does not fail with a missing view error:

Terminal window
php artisan module:make-view show Blog

Wrap routes in your application’s middleware, such as Route::middleware('auth')->group(...). To add custom middleware inside the module, see Add middleware to a module.

Reference routes from other modules by name so views stay decoupled from namespaces and URLs:

Modules/Blog/resources/views/index.blade.php
<a href="{{ route('shop.products.index') }}">Shop</a>

Disabled modules register no routes, so calling route() for a disabled module’s route throws RouteNotFoundException.