简体   繁体   中英

Laravel 5 Middleware not working properly

I have define a middleware in laravel 5. But when I login it redirect me to dashboard but dashboard doesn't show and an error occurred. I have upload a picture of it. http://imgur.com/a/1fCwt

AdminAuth.php

<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class AdminAuth 
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::check())
        {
            $user = Auth::user();
            return redirect()->route('admin_display_dashboard'); 
        }  else {
            return redirect()->route('admin_display_login');
        }
    }
}   

Kernel.php

<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel {
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],
        'api' => [
            'throttle:60,1',
        ],
    ];
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'adminAuth' => \App\Http\Middleware\AdminAuth::class,
    ];
}

Route.php

Route::group(['middleware' => ['adminAuth']], function () {

Route::get('admin/dashboard', array('as'=>'admin_display_dashboard', 'uses'=>'AdminViewController@displayDashboard'));

});

what's in your admin_display_dashboard() and admin_display_login(), you sure there is no redirect or so in there.

Meanwhile try this

 if(!Auth::check())
 {

    return redirect()->route('admin_display_login');
 }

 $user = Auth::user();

 return $next($request);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM