laravel哨兵重定向::预期无法正常工作

射线

我正在尝试在我的应用程序中正确设置哨兵包裹。

我可以登录和注销用户并保护路由,但似乎无法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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

重定向无法按预期在Rails中正常工作

来自分类Dev

使用Laravel 5无法正常工作的消息进行重定向

来自分类Dev

Heroku Laravel上的HTTPS重定向无法正常工作

来自分类Dev

使用Laravel 5无法正常工作的消息进行重定向

来自分类Dev

Laravel 5.4 外部 url 重定向无法正常工作

来自分类Dev

.htaccess www重定向无法正常工作

来自分类Dev

重定向301 htaccess无法正常工作

来自分类Dev

重定向301无法正常工作codeigniter

来自分类Dev

javascript重定向仍然无法正常工作

来自分类Dev

301动态重定向无法正常工作

来自分类Dev

301重定向无法正常工作

来自分类Dev

目录的HTTPS重定向无法正常工作

来自分类Dev

重定向htaccess无法正常工作

来自分类Dev

重定向后,TempData []无法正常工作

来自分类Dev

co重定向无法正常工作

来自分类Dev

重定向到文件无法正常工作

来自分类Dev

WordPress重定向无法正常工作

来自分类Dev

Codeigniter重定向无法正常工作

来自分类Dev

javascript警报重定向无法正常工作

来自分类Dev

Nginx 301 重定向无法正常工作

来自分类Dev

登录时重定向无法正常工作

来自分类Dev

.htacess 重定向 301 无法正常工作

来自分类Dev

预期无法正常工作

来自分类Dev

RewriteRule在SSL重定向上无法正常工作

来自分类Dev

使用$ this-> referer()的cakePHP重定向无法正常工作

来自分类Dev

Shell脚本中的输出重定向无法完全正常工作

来自分类Dev

htaccess 301重定向无法正常工作

来自分类Dev

WordPress:301重定向无法正常工作

来自分类Dev

HTTP到HTTPS重定向-IIS 8.5无法正常工作