我记得你的最后一个问题,但由于你没有提供更新,我也无法发布任何内容。尽管如此,你让它变得比现在更复杂。
Route.php
Route::get('login', 'LoginController@getLogin');
Route::post('login', 'LoginController@postLogin');
Route::group(['before' => 'authentication'], function(){
Route::get('profile' => 'ProfileController@profile');
//All protected routes here
});
我在这里所做的是,我创建了一个过滤器authentication,它将针对组内的所有路由运行。
现在,让我们定义过滤器。
Route::filter('authentication', function()
{
if ( ! Sentry::check())
{
return Redirect::action('LoginController@getLogin');
}
});
这只是一个简单的过滤器,它将检查用户是否登录。如果用户没有登录,它会将用户重定向到login 路由,表单将在该路由处提供。
控制器:
public function getLogin()
{
if(Sentry::check())
{
return Redirect::action('ProfileController@profile');
}
return View::make('login');
}
public function postLogin()
{
//put the validation rules here and validate. as far as i remember, you know how to do it.
if($validator->passes())
{
try
{
// Login credentials
$credentials = array(
'email' => 'john.doe@example.com',
'password' => 'password',
);
// Authenticate the user
$user = Sentry::authenticate($credentials, false);
return Redirect::action('ProfileController@profile');
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
echo 'User is not activated.';
}
// The following is only required if the throttling is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
echo 'User is suspended.';
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
echo 'User is banned.';
}
}
}
在 catch 块中,执行所需的操作。例如如果您想从错误重定向到登录,则将错误添加到消息包中(如果您不知道如何,请click here 了解详细信息)并重定向到登录表单。
或者,如果它是 ajax 数据,您可以将错误作为 json 返回,然后在客户端解析它们,同时在 ajax 失败时显示错误消息。
如果用户没有登录,那么访问所有这些受保护的路由将引发重定向,并且用户将被重定向到登录表单。成功登录后,他将被重定向到他的个人资料页面。另一方面,如果登录用户尝试转到登录表单,那么他将被重定向到个人资料页面,因为登录用户不应该看到登录表单。
更新 #1
这比你想象的要容易。
伪代码。
- 第一次检查用户是否登录。如果没有,则将他重定向到登录页面。
- 2,如果他登录了,当他去url(球队的密码页面)时,检查他是否是队长(数据库调用)。如果不是,则将他重定向到其他页面或向他显示 403 禁止页面。
- 3rd,如果他是队长,请给他看表格。并设置一些会话,以便在后续调用中,您可以参考该令牌检查授权。
- 4、如果认证正确,则将他带到编辑页面。否则,带他到第 3 步,并显示错误消息,以便该人知道输入密码时出错。