2016-04-25 215 views
0

在我的項目中我需要保護一些視圖。我在現場控制user auth Laravel 5.2

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

//Spot 
    Route::get('administrator/spot-new', '[email protected]'); 
    Route::post('administrator/spot-new', '[email protected]'); 
} 

: 我創建了一個路由器組

public function __construct() 
{ 
    $this->middleware('auth'); 
} 

但是當我嘗試進入現場查看,我不能看到登錄頁面。 我有這個錯誤: 對不起,找不到您正在尋找的頁面。

+0

檢查了這一點,也許它可以幫助你http://stackoverflow.com/questions/36567068/laravel-5-2-auth-registration-page-blocked/36567538#36567538 –

+0

使用[「中間件」 => ['web','auth']] –

+0

in Authenticate middleware if(Auth :: guard($ guard) - > guest()){$ request-> ajax()|| $ request-> wantsJson()){ return response('Unauthorized。',401); } else { return redirect() - > guest('auth/login'); } } –

回答

0

Laravel 5.2添加了中間件組。對於開始會話/加密餅乾/驗證CSRF令牌等等。見下面

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, 
    ], 

You're required to add when working with sessions and any other stuff in that group.

所以要解決你的問題添加 '網絡' 到

https://laravel.com/docs/5.2/middleware#middleware-groups

的Web中間件組負責你的中間件

Route::group(['middleware' => ['web', 'auth']], function(){ 
    Route::get('administrator/spot-new', '[email protected]'); 
    Route::post('administrator/spot-new', '[email protected]'); 
} 

而在你的控制器構造函數

public function __construct() 
{ 
    //$this->middleware('auth'); (No need for this one) 
} 
相關問題