1、直接在routes文件夹下创建pc.php,代码如下:
<?php use Illuminate\Support\Facades\Route; Route::get('test',function(){ return 'I am pc'; }); ?>
直接用网址进行访问,提示404错误,路由没有被加载,这种方法是行不通的。
2、在web.php的下边,通过
include_once('pc.php')
引入pc.php,网址访问,页面显示
I am pc
路由被正确加载,这种方法是可行的。
3、在app\Providers\RouteServiceProvider.php文件里的61行,找到laravel是如何加载web.php,进行仿写
protected function mapWebRoutes() { Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web.php')); Route::middleware('pc') ->namespace($this->namespace) ->group(base_path('routes/pc.php')); }
网址访问,出现错误
Illuminate\Contracts\Container\BindingResolutionException Target class [pc] does not exist.
原因是pc中间件未被定义,查询web的中间件的定位位置,在app\Http\Kernel.php的32行,进行仿写
'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, ], 'pc' => [ \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, ],
网址访问,页面显示
I am pc
路由被正确加载,这种方法是可行的。
第2和第3那种方法更好用,没有得出结论, 希望老师可以告知下!