In Laravel, you can define a route with a namespace by using the namespace
method within the Route
facade. Here's how you can do it:
Open your routes/web.php
or routes/api.php
file.
Use the Route
facade to define a route with a namespace. The namespace
method allows you to specify the namespace for the controller that will handle the route. Here's an example:
use App\Http\Controllers\Admin\AdminController;
Route::namespace('Admin')->group(function () {
Route::get('/admin', [AdminController::class, 'index']);
});
In this example, we have defined a route with the namespace Admin
. The AdminController
will handle the /admin
route.
use App\Http\Controllers\Admin\AdminController;
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/', [AdminController::class, 'index']);
Route::get('/dashboard', [AdminController::class, 'dashboard']);
});
In this example, we have added a prefix /admin
to both routes. So, the routes will be /admin
and /admin/dashboard
, and both will be handled by the AdminController
.
That's it! You have defined a route with a namespace in Laravel.