美文网首页
Laravel 新增路由文件

Laravel 新增路由文件

作者: 自奏圣乐 | 来源:发表于2019-06-12 00:11 被阅读0次

    /app/Http/Controller/文件夹下创建了一个Admin文件夹

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

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

      添加路由方法

        protected function mapAdminRoutes()
        {
               Route::prefix('admin')
    
                    ->middleware('admin')
    
                    ->namespace($this->namespace.'\Admin')
    
                    ->group(base_path('routes/admin.php'));
         }
    

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

          public function map()
           {
                $this->mapApiRoutes();
    
                $this->mapWebRoutes();
    
                $this->mapAdminRoutes();    // 添加执行的路由方法
            }
    
    1. /app/Http/Kernel.php中添加admin类名及其路径
        protected $routeMiddleware = [
            'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
            'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
            'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
            'can' => \Illuminate\Auth\Middleware\Authorize::class,
            'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
            'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
            // 根据之前设置的路由规则名(admin)对应添加admin类名,并指向路由验证路径
            'admin' => \App\Http\Middleware\VerifyAdmin::class,
        ];
    
    1. /app/Http/Middleware/文件夹下创建VerifyAdmin.php,并写入验证代码
        <?php
        namespace App\Http\Middleware;
    
        use Closure;
    
        class VerifyAdmin
        {
    
           public function handle($request, Closure $next)
            {
                 // if ("判断条件") {
                        return $next($request);
                  // }
                    // 返回跳转到网站首页
                    // return redirect('/');
            }
    
        }
    

    这样就新增了一个名为admin的路由文件

    相关文章

      网友评论

          本文标题:Laravel 新增路由文件

          本文链接:https://www.haomeiwen.com/subject/otuhfctx.html