我在Laravel 4.2中使用multiauth和两个表admin_profile和公司。
public function Login()
{
$LoginData = Input::except(array('_token'));
//return $LoginData;
if (Input::get('username')=='' || Input::get('password')=='')
{
if(!$LoginData['CompanyURL']=='')
return Redirect::to('')->withInput()->with('Message', 'Enter Username and Password');
else
return Redirect::to('company/'.$LoginData['CompanyURL'])->withInput()->with('Message', 'Enter Username and Password');
}
//Admin User
if($LoginData['CompanyURL']=='')
{
if (Auth::admin()->attempt(array_filter($LoginData)))
{
$UserDetails = User::where('username', Input::get('username'))->first();
if(count($UserDetails)>0)
return Redirect::intended('home');
}
else
return Redirect::to('')->withInput()->with('Message', 'UserName or Password Invalid');
}
else
{
if (Auth::user()->attempt($LoginData))
{
//return $LoginData;
$CompanyModel = User::where('username', Input::get('username'))->first();
return Redirect::intended('company/'.$LoginData['CompanyURL'].'/home');
}
return Redirect::to('company/'.$LoginData['CompanyURL'])->withInput()->with('Message', 'UserName or Password Invalid');
}
}
但我无法检查是否有任何类型为admin或company的用户登录以打开需要用户身份验证的主页等页面
如何改变这一点 Route :: group(array('before'=>'auth'),function() { 一些路线...... }
答案 0 :(得分:2)
您必须更改过滤器,请查看示例,了解如何修复过滤器https://gist.github.com/ollieread/8303638
此外,如果您只想对常用页面使用一个auth过滤器,那么就有解决方案,
Route::filter('auth', function()
{
$guest = true;
if (Auth::student()->guest() && Auth::teacher()->guest() && Auth::parent()->guest()){
$guest = false;
}
if(!$guest){
return Redirect::to('login');
}
});
我还建议您定义不同类型的过滤器,这些过滤器对于拥有路径权限的人非常有用,
Route::filter('student', function()
{
if (!Auth::student()->check() ) // Checks the current user
{
return Redirect::to('login');
}
});
Route::filter('teacher', function()
{
if (! Auth::teacher()->check() ) // Checks the current user
{
return Redirect::to('login');
}
});