Laravel中间件不适用于路由组

时间:2018-07-24 10:42:28

标签: php laravel routes laravel-middleware

我已经创建了一个路由组:

Route::group(['prefix' => 'admin', 'middleware' => ['is_admin'], 'namespace' => 'Admin'], function() {
    CRUD::resource('location', 'LocationCrudController');
});

但是当我转到网址site.local/admin时,它会重定向到管理员登录页面。

这是我的自定义中间件文件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class IsAdmin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user() &&  Auth::user()->is_admin == '1') {
            return $next($request);
        }

        return abort(404);
    }
}

php artisan route:list的结果:

   | GET|HEAD                               | admin                                               | backpack                           | Backpack\Base\app\Http\Controllers\AdminController@redirect                          | web,admin 

Kernel.php:

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,
    'is_admin' => \App\Http\Middleware\IsAdmin::class,
];

请帮助。

0 个答案:

没有答案