0
我需要幫助,有人可以告訴我,如果我的工作對於身份驗證用戶配置文件是否正確?我的下一個文件:Laravel身份驗證和過濾器
文件routes.php文件(我只使用了兩個羣體在這個例子中)
<?php
//home
Route::get('/',function()
{
return Redirect::to('login');
});
//login get
Route::get('login','[email protected]');
//login for form
Route::post('login','[email protected]');
//routes for admin
Route::group(array('before' => 'adminFilter'),function()
{
Route::get('/adminHomePage',function()
{
return View::make('adminHome');
});
});
//route for common user
Route::group(array('before' => 'commonUserFilter'),function()
{
Route::get('/commonUserPage',function()
{
return View::make('commonPage');
});
});
Route::get('logout','[email protected]');
?>
文件filters.php
<?php
Route::filter('adminFilter', function($route, $request)
{
if (Auth::user()->profile != 1)
{
return Redirect::to('/logout');
}
});
Route::filter('commonUserFilter',function($route, $request)
{
if (Auth::user()->profile != 2)
{
return Redirect::to('/logout');
}
});
?>
文件AuthController.php
<?php
public function showLogin()
{
return View::make('login');
}
public function postLogin()
{
//Get user data from login form
$user = array(
'user' => Input::get('username'),
'password' => Input::get('password'));
if(Auth::attempt($user,true))
{
switch (Auth::user()->profile)
{
case 1:
//home admin
return Redirect::to('/adminHomePage');
break;
case 2:
//home common user
return Redirect::to('/commonUserPage');
break;
}
}
else
{
return Redirect::to('login')
->with('mensaje_error','Incorrect data.')
->withInput();
}
}
public function logOut()
{
Auth::logout();
return Redirect::to('/login')
->with('mensaje_error', 'Your session was closed.');
}
?>
爲什麼你不測試它,看看會發生什麼?看起來對我來說是正確的 – Laurence 2014-10-06 22:20:28
我會將用戶重定向到主頁,而不是在他們無法查看頁面時將其註銷。當然,這取決於你在做什麼。 – Jerodev 2014-10-07 06:54:26
感謝您的回覆,我運行該示例,它工作正常。我被張貼了這個接收意見和建議,並可能顯示一種方法來進行配置文件管理。 – Turcko007 2014-10-07 16:40:17