Laravel 5.6中如何使用多路由文件?含web与admin路由配置
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
webandadminInformationmiddleware to all routes inadmin.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:
- Create the middleware file: Run
php artisan make:middleware AdminInformation, then openapp/Http/Middleware/AdminInformation.phpand 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); }
- Register the middleware: Open
app/Http/Kernel.phpand add this line to the$routeMiddlewarearray:
'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

