Laravel New Middleware无法正常工作

时间:2017-03-01 05:04:43

标签: laravel laravel-routing laravel-5.4 laravel-middleware

我的项目有问题,我只是尝试在很长时间内修复,但仍然无法正常工作。

我创建了一个新的中间件 - 这是我的代码:

    class CpanelAuthentication
        {
            public function handle($request, Closure $next, $guard = 'player')
            {
                if (Auth::guard($guard)->check()) {
                    return redirect('cpanel');
                }
                return $next($request);
            }
        }

我只是配置提供商和后卫。 - 这是我的auth.php

    'guards' => [
                    'web' => [
                        'driver' => 'session',
                        'provider' => 'users',
                    ],

                    'api' => [
                        'driver' => 'token',
                        'provider' => 'users',
                    ],

                    'player' => [
                        'driver' => 'session',
                        'provider' => 'player',
                    ],
                ],

           'providers' => [
                'users' => [
                    'driver' => 'database',
                    'table' => 'tbl_users'
                ],

                'player' => [
                    'driver' => 'database',
                    'table' => 'tbl_player'
                ],
            ],

我也在Kernel.php中注册了这个中间件 - 这是我的Kernel.php

protected $middlewareGroups = [
        '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,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],

        'player' => [
            \App\Http\Middleware\CpanelAuthentication::class,
        ],
    ];

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

最后我把这个中间件放在这样的路由组中:

Route::group(['middleware'  => 'player'], function() {
        Route::group(['prefix' => 'cpanel', 'namespace' => 'Cpanel'], function() {
            Route::get('/', [
                'as'    => 'getCpanel',
                'uses'  => 'CpanelController@getCpanel'
            ]);
            Route::group(['prefix' => 'investment'], function() {
                Route::get('/', [
                    'as' => 'getCpanelInvestment',
                    'uses' => 'InvestmentController@getCpanelInvestment'
                ]);
            });
        });
    });

但中间件无法正常工作。当我尝试访问网址时

  

的cPanel /投资

尽管用户未经过身份验证,它仍会通过中间件并重定向!

任何人都可以帮助我!

非常感谢!

2 个答案:

答案 0 :(得分:0)

您正在调用check()来检查用户是否经过身份验证。

如果您想查看他们是否已登录,请改为呼叫Auth::guard($guard)->guest()

答案 1 :(得分:0)

<强>中间件:

 class CpanelAuthentication
   {
            public function handle($request, Closure $next, $guard = 'player')
            {
                if (Auth::guard($guard)->check()) {
                     return $next($request);
                }
                 return redirect('/login');
            }
    }

<强>路线:

Route::group(['middleware' => ['App\Http\Middleware\CpanelAuthentication']], function () {
         //routes
});

尝试不在内核中注册