2017-06-03 220 views
1

我有以下的web.php代碼Laravel中間件不laravel工作5.4

<?php 

namespace App\Http\Middleware; 

use Closure; 
use Session; 

class CheckLogin 
{ 
    /** 
    * Handle an incoming request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Closure $next 
    * @return mixed 
    */ 
    public function handle($request, Closure $next) 
    { 
     if(!Session::has('userAuthDetail')){ 
      // return redirect('login'); 
      // return redirect()->guest('/login'); 
     } else { 
      return $next($request);  
     } 
    } 
} 

Route::get('/login', ['as' => 'login', 'uses' => 'Auth\[email protected]']); 

Route::group(['middleware' => ['login']], function() { 
    Route::post('/add','[email protected]'); 
}); 

登錄控制器

public function postLogin(Request $request) { 
    // print_r($request->all()); 

    $data['email']=$request->email; 
    $data['password']=$request->password; 
    $response= ApiModel::userLogin($data); 
    if($response->msg=='success'){ 
     session(['userAuthDetail' => $response->data[0]]); 
     return redirect('/'); 
    } else if($response->msg=='failed'){ 
     return redirect('/'); 
    } 
} 

如果我添加如果中間件其他條件,那麼網站投擲錯誤

ErrorException在VerifyCsrfToken.php行156:試圖如果我刪除的if else並添加只是

public function handle($request, Closure $next) 
{ 
    if(!Session::has('userAuthDetail')){ 

    } 
    return $next($request);  
} 

則做工精細獲得非對象

的財產 但如果再失敗的憑據其重定向到儀表板。

注:我不能使用laravel權威性,因爲我與這是寫在PHP核心的API通信

更新;

kernal.php

<?php 

namespace App\Http; 

use Illuminate\Foundation\Http\Kernel as HttpKernel; 

class Kernel extends HttpKernel 
{ 
    /** 
    * The application's global HTTP middleware stack. 
    * 
    * These middleware are run during every request to your application. 
    * 
    * @var array 
    */ 
    protected $middleware = [ 
     \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
     \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 
     \App\Http\Middleware\TrimStrings::class, 
     \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 
    ]; 

    /** 
    * The application's route middleware groups. 
    * 
    * @var array 
    */ 
    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, 
     \App\Http\Middleware\CheckLogin::class, 
     ], 

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

    /** 
    * The application's route middleware. 
    * 
    * These middleware may be assigned to groups or used individually. 
    * 
    * @var array 
    */ 
    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, 
     'login' => \App\Http\Middleware\CheckLogin::class, 
    ]; 
} 
+0

你有沒有在中間件中定義poslogin函數路由? – kunal

+0

@kunal .no其登錄中間路線外 – vision

+0

您是如何在Kernel.php文件中註冊中間件的? –

回答

0

如果用戶會話沒有

public function handle($request, Closure $next) 
{ 
    if(!Session::has('userAuthDetail')){ 
     //return to login page 
    } 
    return $next($request);//if session exist then forward the request  
} 
+0

雅我已經嘗試過,但如果我補充說,那麼網站頁面沒有響應 – vision

0

Line 156 of VerifyCsrfToken.php創建您應該返回到登錄頁面顯示,它試圖調用$response->headers->setCookie(...)。您的錯誤表明$ response爲空,這就是if(!Session::has('userAuthDetail')) { ... }場景中的CheckLogin中間件會發生的情況。

基本上,你有一個返回null的中間件(或者什麼都沒有,哪種在PHP世界中被視爲null)。這是不允許的,中間件必須總是返回某種響應(或拋出異常)。

TL; DR:您的中間件必須返回所有執行路徑的響應。