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):
php artisan module:make-controller Post Blogphp artisan module:make-controller Api/Post Blog --apiSee 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.
-
Add a web route for
PostControllerinModules/Blog/routes/web.php. Keep the generatedRoute::resource()line if you still need theblogsroutes.Modules/Blog/routes/web.php <?phpuse 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.
-
Replace
Modules/Blog/routes/api.phpwith a public API route withoutauth:sanctum.RouteServiceProvideralready applies theapiURL prefix andapi.name prefix.Modules/Blog/routes/api.php <?phpuse 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/postsunder the nameapi.blog.posts.index. -
Check the result.
Terminal window php artisan route:list --path=postsGET|HEAD api/v1/posts ......... api.blog.posts.index › Modules\Blog\Http\Controllers\Api\PostController@indexGET|HEAD posts ................ blog.posts.index › Modules\Blog\Http\Controllers\PostController@index
Add a URL prefix
Section titled “Add a URL prefix”Wrap module routes in prefix() and name() to group them under a shared path and name prefix:
<?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:
php artisan module:make-view show BlogRequire authentication
Section titled “Require authentication”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.
Link to a route in another module
Section titled “Link to a route in another module”Reference routes from other modules by name so views stay decoupled from namespaces and URLs:
<a href="{{ route('shop.products.index') }}">Shop</a>Disabled modules register no routes, so calling route() for a disabled module’s route throws RouteNotFoundException.
Related
Section titled “Related”module:route-provider: regenerate theRouteServiceProvider.- Module service provider: what the module provider registers.
- Add controllers to a module
- Add middleware to a module