laravel sentry 重定向::预期不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19061507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel sentry redirect::intended not working
提问by Ray
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.
我可以让用户登录和退出并保护路由,但我似乎无法redirect::intended
正常工作。我的理解是,用户将被带回他们最初调用的路线,然后再被定向到登录页面。目前它只是不断重定向到默认页面。
In my routes.php I have the following group set up:
在我的 routes.php 中,我设置了以下组:
Route::group(array('before' => 'sentryAuth'), function () {...}
Within this group I've placed all the protected routes.
在这个组中,我放置了所有受保护的路由。
In my filters.php I have the following filters:
在我的 filters.php 中,我有以下过滤器:
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
return Redirect::route('login');
}
});
Route::filter('sentryGuest', function () {
路线::过滤器('哨兵客人',功能(){
if (Sentry::check()) {
return Redirect::intended('dashboard');
}
});
In my userController I have the following code:
在我的 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');
}
}
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.
我试图在未登录的情况下访问“预订/创建”页面。我进入登录页面,登录但它随后将我带到仪表板而不是预订/创建。
AM I missing something here? Is there additional code I need to get the intended to work??
我在这里错过了什么吗?是否需要额外的代码才能实现预期的工作?
回答by Tom
In your filters.php make sure to use:
在您的 filters.php 中确保使用:
return Redirect::guest('login');
instead of
代替
return Redirect::route('login');
The guest function will set the correct session variables for intended() to work properly.
来宾函数将为 expected() 设置正确的会话变量以使其正常工作。
回答by reikyoushin
I am not sure about this, because I am not using Sentry for my current project.. so this is just a hunch.
我不确定这一点,因为我没有在当前项目中使用 Sentry.. 所以这只是一种预感。
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 Redirectorclass and i saw this:
似乎由于您使用了 SentryAuth 而不是 laravel 4 中的本机 Auth 类,因此未设置预期 url 的会话项。我查看了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);
}
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..
正如代码所说,会话密钥是“url.intended”。我使用本机 Auth 过滤器对此进行了验证,并且预期的 URL 已Session::get('url.intended')
按预期设置。
so a possible solution to this is to manually set it in the session. an example would be:
所以一个可能的解决方案是在会话中手动设置它。一个例子是:
On your filter
在你的过滤器上
Route::filter('sentryAuth', function () {
if (!Sentry::check()) {
Session::put('loginRedirect', Request::url());
return Redirect::route('login');
}
});
On your postAuthenticate() method
在您的 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);
}
parts of the code were taken from herefor reference.. ^_^
部分代码取自here以供参考..^_^
回答by morphatic
@reikyoushin's answer is excellent. Here's a slightly different version.
@reikyoushin 的回答非常好。这是一个略有不同的版本。
routes.php
路由文件
// NOTE: do NOT name your filter "auth" as it will not override
// Laravel's built-in auth filter and will not get executed
Route::filter( 'sentryAuth', function()
{
// check if logged in or not
if ( ! Sentry::check() )
{
// the guest() method saves the intended URL in Session
return Redirect::guest( 'user/login' );
} else {
// now check permissions for the given route
$user = Sentry::getUser();
if ( ! $user->hasAccess( Route::currentRouteName() ) )
{
// redirect to 403 page
return Response::make( 'Forbidden', 403 );
}
}
});
// Protected routes
Route::group( array( 'before' => 'sentryAuth', function()
{
Route::get( 'admin', function() { return View::make( 'admin.index' ); } );
});
and in your login function:
并在您的登录功能中:
public function login() {
try {
$creds = array(
'email' => Input::get( 'email' ),
'password' => Input::get( 'password' )
);
$user = Sentry::authenticate( $creds );
return Redirect::intended( 'dashboard' );
} catch ( Exception $e ) {
// handle exceptions
}
}
回答by Anam
The easiest way.
最简单的方法。
In your Auth filter use the following:
在您的 Auth 过滤器中使用以下内容:
Route::filter('sentryAuth', function()
{
if ( ! Sentry::check())
{
return Redirect::guest(route('login'));
}
});
In your controller:
在您的控制器中:
public function postAuthenticate()
{
try {
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$user = Sentry::authenticate($credentials, false);
} catch () {
// validation errors
}
//firstly, Laravel will try to redirect intended page.
//if nothing saved in session, it will redirect to home.
//you can use any route instead of home.
return Redirect::intended(route('home'));
}
Done.
完毕。
Outside of Sentry:
哨兵外:
This code can be used with any type of authentication library. You need two things to implement intended redirection:
此代码可用于任何类型的身份验证库。您需要两件事来实现预期的重定向:
1.in your auth filter:
1.在您的身份验证过滤器中:
Route::filter('auth', function() { if ( ! Sentry::check()) { return Redirect::guest(route('login')); } });
2.After logged in an user:
2.登录用户后:
//firstly, Laravel will try to redirect intended page. //if nothing saved in session, it will redirect to home. //you can use any url instead of '/' return Redirect::intended('/');