2016-03-04 114 views
1

我有laravel路線的問題。我希望auth註冊路由只能由管理員或登錄用戶訪問。爲了實現這一點,我已經從routes.php文件刪除Route::auth();並創建AUTH中間件我自己的路由表項。Laravel 5.2驗證中註冊用戶只有登錄用戶或管理員只

努力

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

    // Authentication Routes... 
    $this->get('login', 'Auth\[email protected]'); 
    $this->post('login', 'Auth\[email protected]'); 
    $this->get('logout', 'Auth\[email protected]'); 

    // Password Reset Routes... 
    $this->get('password/reset/{token?}', 'Auth\[email protected]'); 
    $this->post('password/email', 'Auth\[email protected]'); 
    $this->post('password/reset', 'Auth\[email protected]'); 

    Route::get('/home', '[email protected]'); 
    Route::post('/ajax/getStates', '[email protected]'); 
    Route::post('/ajax/getCities', '[email protected]'); 


}); 


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

    // Registration Routes... 
    $this->get('register', 'Auth\[email protected]'); 
    $this->post('register', 'Auth\[email protected]'); 

}); 

上面的代碼工作正常。當我試圖訪問註冊網址時,它只是將我重定向到登錄頁面。現在實際問題在登錄後開始。

登錄後,我試圖訪問註冊頁,但它並沒有顯示出來,而不是把它重定向我的家一樣http://localhost/

請給我解決方案。

+0

它,因爲你已經確立了自己的''auth'中間件內註冊route',而不是你需要你的'web'中間件內得到它只有這樣你就可以在不登錄 –

+0

訪問它,我想它應該是通過訪問記錄的用戶,而不是匿名用戶。這就是爲什麼我把它放在auth中間件中。 – Mandy

+0

然後why're您在'AuthController' –

回答

2

檢查AuthController的構造。客戶中間件分配給除logout以外的所有方法。

+0

我知道,我已經通過了getRegister並註冊後進入。但那不適合我,所以我已經從構造函數中刪除了,現在註冊表單工作正常。 – Mandy

+0

@lagbox:謝謝,它適用於我(Laravel 5.2.36) – Sefran2

0

我有一個簡單的解決方案。我不是專家,但我正在以這種方式進行工作,並且按照您的要求工作。

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

Route::get('/edit', function() { 
    if (Auth::user()) { 
     return view('auth.user_edit'); //Page which you want to show for loged user. 
    } else { 
     return "MM"; //You can redirect from here, if user is not logged in 
    } 
}); 
});