laravel哨兵重定向::意图不工作 [英] laravel sentry redirect::intended not working
问题描述
我试图让哨兵包在我的应用程序设置正确。
I'm trying to get the sentry package set up in my app correctly.
我可以在注销用户,并进行保护的路线,但我似乎无法获得重定向::打算
正常工作。我的理解是,用户将被带回的路线,他们被引导到登陆页面之前原名。目前,它只是不断重定向到默认页面。
I can log a user in and out and protect routes but I can't seem to get the redirect::intended
to work properly. My understanding is that a user will be taken back to the route they originally called before being directed to the login page. At the moment it simply keeps redirecting to the default page.
在我的routes.php文件我有以下组设置:
In my routes.php I have the following group set up:
Route::group(array('before' => 'sentryAuth'), function () {...}
在这一组中,我放所有受保护的途径。
Within this group I've placed all the protected routes.
在我filters.php我有以下过滤器:
In my filters.php I have the following filters:
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
return Redirect::route('login');
}
});
路线::过滤器('sentryGuest',函数(){
Route::filter('sentryGuest', function () {
if (Sentry::check()) {
return Redirect::intended('dashboard');
}
});
在我的UserController中,我有以下code:
In my userController I have the following code:
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');
}
}
我试图访问一个页面预约/创建没有被记录。我得到采取到登录页面,登录但随后带我到仪表板不要预订/创建。
I've tried to access a page 'bookings/create' without being logged in. I get taken to the login page, log in but it then takes me to the dashboard not to bookings/create.
我失去了一些东西在这里?是否有额外的code我需要得到预期的工作??
AM I missing something here? Is there additional code I need to get the intended to work??
推荐答案
我不知道这事,因为我没有使用哨兵为我的当前项目..所以这只是一种预感。
I am not sure about this, because I am not using Sentry for my current project.. so this is just a hunch.
看来,既然你使用SentryAuth,而不是本地验证类laravel 4,对预期的URL会话项没有设置..我看着就上的重定向类,我看到这样的:
It seems that since you used SentryAuth and not the native Auth class in laravel 4, the session item for the intended url is not set.. I looked on the API on the Redirector class and i saw this:
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);
}
和为code称,会话密钥url.intended。我证实了这一使用本地验证过滤器和目的URL设置在会话::得到('url.intended')
如预期..
and as the code says, the session key is 'url.intended'. i verified this using the native Auth filter and the intended URL is set on Session::get('url.intended')
as expected..
这样一个可能的解决方案是手动设置在会话中。一个例子是:
so a possible solution to this is to manually set it in the session. an example would be:
在过滤器
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);
}
在code部分是从这里参考采取.. ^ _ ^
parts of the code were taken from here for reference.. ^_^
这篇关于laravel哨兵重定向::意图不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!