Blogger Information
Blog 9
fans 0
comment 0
visits 7480
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
尝试把web.php路由文件分成多个
高杰
Original
686 people have browsed it

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那种方法更好用,没有得出结论, 希望老师可以告知下!

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:每个方法都有自己的应用场景
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post