我正在尝试在我的应用程序中正确设置哨兵包裹。
我可以登录和注销用户并保护路由,但似乎无法redirect::intended
正常工作。我的理解是,在定向到登录页面之前,用户将被带回到他们最初调用的路由。目前,它只是一直重定向到默认页面。
在我的routes.php中,我设置了以下组:
Route::group(array('before' => 'sentryAuth'), function () {...}
在该组中,我放置了所有受保护的路由。
在我的filters.php中,我具有以下过滤器:
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
return Redirect::route('login');
}
});
Route :: filter('sentryGuest',function(){
if (Sentry::check()) {
return Redirect::intended('dashboard');
}
});
在我的userController中,我有以下代码:
public function postAuthenticate()
{
try {
// Set login credentials
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
// Try to authenticate the user
$user = Sentry::authenticate($credentials, false);
} 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\UserNotFoundException $e) {
echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e) {
echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e) {
echo 'User is not activated.';
}
if (!Sentry::check()) {
return Redirect::to('user/login');
} else {
return Redirect::intended('dashboard');
}
}
我尝试不登录而访问“预订/创建”页面。我进入登录页面,登录,但是随后我转到仪表板,而不是预订/创建。
我在这里想念什么吗?我是否需要其他代码才能正常工作?
我对此不太确定,因为我没有在当前项目中使用Sentry。
看来,由于您使用的是SentryAuth,而不是laravel 4中的本机Auth类,因此未设置预期网址的会话项。。我查看了Redirector类的API,然后看到了:
public function intended($default, $status = 302, $headers = array(), $secure = null)
{
$path = $this->session->get('url.intended', $default);
$this->session->forget('url.intended');
return $this->to($path, $status, $headers, $secure);
}
如代码所示,会话密钥为“ url.intended”。我使用本机Auth过滤器对此进行了验证,并且预期的网址已Session::get('url.intended')
按预期设置。
因此可能的解决方案是在会话中手动设置它。一个例子是:
在您的过滤器上
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
Session::put('loginRedirect', Request::url());
return Redirect::route('login');
}
});
在您的postAuthenticate()方法上
if (!Sentry::check()) {
return Redirect::to('user/login');
} else {
// Get the page we were before
$redirect = Session::get('loginRedirect', 'dashboard');
// Unset the page we were before from the session
Session::forget('loginRedirect');
return Redirect::to($redirect);
}
部分代码是从此处获取的,以供参考。^ _ ^
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句