• Laravel5.5添加新路由文件并制定规则


    Laravel5.5里面有4个默认的路由文件,其中web.php是默认路由文件,如果需要添加其他路由文件,按照以下步骤进行。

    此处以添加网站home前端路由举例,我已经先在/app/Http/Controller/文件夹下创建了一个Home文件夹,这个文件夹下主要放网站前端控制器,其他步骤如下:

    1. 在项目routes目录下添加路由文件home.php;

    2. 修改/app/providers/RouteServiceProvider.php

        (1)添加路由方法

    protected function mapHomeRoutes()
    {
        Route::prefix('home')
             ->middleware('home')
             ->namespace($this->namespace.'Home')
             ->group(base_path('routes/home.php'));
    }
    
    如果不想添加路由前缀可以去掉
    protected function mapHomeRoutes()
        {
            Route::namespace($this->namespace.'Home')
                ->group(base_path('routes/home.php'));
        }

      (2)将添加的路由方法加入map方法中执行

    public function map()
    {
        $this->mapApiRoutes();
        $this->mapWebRoutes();
        $this->mapHomeRoutes();    // 添加执行的路由方法
    }

    附完整的RouteServiceProvider.php代码:

    <?php
     
    namespace AppProviders;
     
    use IlluminateSupportFacadesRoute;
    use IlluminateFoundationSupportProvidersRouteServiceProvider as ServiceProvider;
     
    class RouteServiceProvider extends ServiceProvider
    {
        /**
         * This namespace is applied to your controller routes.
         *
         * In addition, it is set as the URL generator's root namespace.
         *
         * @var string
         */
        protected $namespace = 'AppHttpControllers';
     
        /**
         * Define your route model bindings, pattern filters, etc.
         *
         * @return void
         */
        public function boot()
        {
            //
     
            parent::boot();
        }
     
        /**
         * Define the routes for the application.
         *
         * @return void
         */
        public function map()
        {
            $this->mapApiRoutes();
     
            $this->mapWebRoutes();
     
            $this->mapHomeRoutes();
        }
     
        /**
         * Define the "web" routes for the application.
         *
         * These routes all receive session state, CSRF protection, etc.
         *
         * @return void
         */
        protected function mapWebRoutes()
        {
            Route::middleware('web')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/web.php'));
        }
     
        /**
         * Define the "api" routes for the application.
         *
         * These routes are typically stateless.
         *
         * @return void
         */
        protected function mapApiRoutes()
        {
            Route::prefix('api')
                 ->middleware('api')
                 ->namespace($this->namespace)
                 ->group(base_path('routes/api.php'));
        }
     
        protected function mapHomeRoutes()
        {
             Route::prefix('home')
                 ->middleware('home')
                 ->namespace($this->namespace.'Home')
                 ->group(base_path('routes/home.php'));
        }
     
    }

    3. 在/app/Http/Kernel.php中添加home类名及其路径

    protected $routeMiddleware = [
        'auth' => IlluminateAuthMiddlewareAuthenticate::class,
        'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,
        'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,
        'can' => IlluminateAuthMiddlewareAuthorize::class,
        'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,
        'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,
        // 根据之前设置的路由规则名(home)对应添加home类名,并指向路由验证路径
        'home' => AppHttpMiddlewareVerifyHome::class,
    ];

    4. 在/app/Http/Middleware/文件夹下创建VerifyHome.php,并写入验证代码

    代码如下:

    <?php
    namespace AppHttpMiddleware;
    use Closure;
     
    class VerifyHome
    {
        public function handle($request, Closure $next)
        {
            // if ("判断条件") {
                return $next($request);
            // }
                
            // 返回跳转到网站首页
            // return redirect('/');
        }
    }

    上面没有执行对home路由请求的验证,如果有需要自己加上。

    5. 测试举例

        (1)在home.php路由里添加两条路由规则,代码如下:

    <?php
    Route::get('index', 'IndexController@index');

    (2)在/app/Http/Controller/Home/文件夹下创建IndexController.php,创建方式可以直接在文件夹下创建文件,也可以使用工具匠( php artisan make:controller HomeUserController ),控制器代码如下:

    <?php
    
    namespace AppHttpControllersHome;
    
    use AppHttpControllersController;
    use AppUser;
    use IlluminateHttpRequest;
    
    class IndexController extends Controller
    {
        public function index()
        {
            echo "111222";
        }
    }

    (3)访问测试:

    有路由前缀

    没有路由前缀

    注意:访问默认路由web.php下的规则不用加web,访问其他路由文件需要加上在RouteServiceProvider.php中定义的路由名。

     ok,可以按照你的模块架构分路由文件了!

    参考链接:https://blog.csdn.net/createNo_1/article/details/81035154

  • 相关阅读:
    POJ 3904 Sky Code (容斥原理)
    HDU 4334 Trouble (暴力)
    UVA 10325 The Lottery( 容斥原理)
    HDU 2841 Visible Trees 数论+容斥原理
    UVA11806-Cheerleaders(容斥原理+二进制)
    HDU1695 GCD (欧拉函数+容斥原理)
    HDU 5651 xiaoxin juju needs help (组合数)
    最大子矩阵和 51Nod 1051 模板题
    最大子段和 模板题 51Nod 1049
    51Nod 1006 最长公共子序列Lcs问题 模板题
  • 原文地址:https://www.cnblogs.com/clubs/p/11194570.html
Copyright © 2020-2023  润新知