You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.6中如何使用多路由文件?含web与admin路由配置

How to Properly Use Your Custom Admin Routes in Laravel 5.6

Hey there! Let's break down exactly how to work with your web and admin routes based on the setup you've started in Laravel 5.6.

1. First, Finish Your RouteServiceProvider Configuration

It looks like your mapAdminRoutes() method got cut off. Make sure it's fully completed so Laravel can load your admin.php routes correctly:

protected function mapAdminRoutes() { 
    Route::middleware(['web', 'adminInformation']) 
        ->prefix('manage') 
        ->namespace($this->namespace) 
        ->group(base_path('routes/admin.php')); 
}

This tells Laravel:

  • Apply the web and adminInformation middleware to all routes in admin.php
  • Add a manage/ URL prefix to every admin route
  • Use your app's default namespace (usually App\Http\Controllers) for the controllers referenced in admin routes

2. Define Routes in admin.php

You can define routes in admin.php just like you do in web.php, but you don't need to re-add the middleware or prefix—those are already handled by the RouteServiceProvider.

Example routes/admin.php content:

// Dashboard route
Route::get('/', 'AdminController@dashboard')->name('admin.dashboard');

// Resource route for managing users
Route::resource('users', 'Admin\UserController');

// A simple route for editing settings
Route::get('/settings', 'Admin\SettingsController@edit')->name('admin.settings.edit');
Route::post('/settings', 'Admin\SettingsController@update')->name('admin.settings.update');

3. Set Up the adminInformation Middleware

Since you're using the adminInformation middleware, you need to make sure it's created and registered:

  1. Create the middleware file: Run php artisan make:middleware AdminInformation, then open app/Http/Middleware/AdminInformation.php and add your admin validation logic. For example:
public function handle($request, Closure $next)
{
    // Check if the user is authenticated and has admin privileges
    if (!auth()->check() || !auth()->user()->is_admin) {
        abort(403, 'You don\'t have permission to access this area.');
    }

    return $next($request);
}
  1. Register the middleware: Open app/Http/Kernel.php and add this line to the $routeMiddleware array:
'adminInformation' => \App\Http\Middleware\AdminInformation::class,

4. Access and Use the Routes

Visiting Routes Directly

Thanks to the manage prefix, your admin routes will be accessible at URLs like:

  • /manage → Admin dashboard
  • /manage/users → User management list page
  • /manage/settings → Settings edit page

Generating URLs with Route Names

Using named routes is the cleanest way to reference admin routes in views or controllers. For example:

  • In a Blade view:
<a href="{{ route('admin.dashboard') }}">Admin Dashboard</a>
<a href="{{ route('admin.users.index') }}">Manage Users</a>
  • In a controller redirect:
return redirect()->route('admin.settings.edit');

Optional: Group Routes for Consistent Naming

To avoid typing admin. in every route name, wrap your admin routes in a named group. Update routes/admin.php like this:

Route::name('admin.')->group(function() {
    Route::get('/', 'AdminController@dashboard')->name('dashboard');
    Route::resource('users', 'Admin\UserController');
    Route::get('/settings', 'Admin\SettingsController@edit')->name('settings.edit');
    Route::post('/settings', 'Admin\SettingsController@update')->name('settings.update');
});

Now your route names will automatically be prefixed with admin. (e.g., admin.dashboard, admin.users.index), keeping things organized.

5. Verify Your Routes

Run this Artisan command to see all registered routes and confirm your admin routes are set up correctly:

php artisan route:list

You should see all your admin routes listed with the manage prefix, the correct middleware, and their names.

内容的提问来源于stack exchange,提问作者mySun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:34:22