目录
larvael框架路由核心依赖于服务提供者 app/Providers/RouteServiceProvider.php
因此,自定义路由也主要集中于RouteServiceProvider.php文件
一、新增路由文件
routes 目录下新增路由文件 admin.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/test',function (){
return "欢迎来到admin路由";
});
二、配置
打开 RouteServiceProvider.php文件
新增方法:
protected function mapAdminRoutes()
{
Route::prefix('admin')
->middleware('admin')
->namespace($this->namespace)
->group(base_path('routes/admin.php'));
}
// 新增最后一句!
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapAdminRoutes();
//
}
三、注册中间件
打开 app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Session\Middleware\StartSession::class,
],
//添加这句!!
'admin'=>[
]
];
第三步如果不执行,则路由会报错:
Illuminate\Contracts\Container\BindingResolutionException
Target class [admin] does not exist.
四、访问结果展示