CakePHP认证登录重定向到错误的地方 [英] CakePHP Auth login redirecting to wrong places

查看:146
本文介绍了CakePHP认证登录重定向到错误的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序设置为始终重定向到登录屏幕。

My application is set up to always redirect to a login screen. The login and logout redirects are driving me nuts though.

当我实际登录时,首先它将重定向到注销URL,然后如果我第二次登录,它将重定向到主页。

When I actually login, first it will redirect to the logout URL, then if I login a second time it will redirect to the home page correctly. Then when I log out, it redirects to the login URL and not the logout URL.

在app_controller.php

public function beforeFilter() {
    $this->Auth->userModel = 'User';
    $this->Auth->loginAction = '/users/login';
    $this->Auth->loginRedirect = '/home';
    $this->Auth->logoutRedirect = '/users/login/1';
    $this->Auth->authError = 'You must be logged in to view this page.';
}

在users_controller.php

public function login($loggedout = false) {
    if ($this->Session->check('Message.auth')) {
        $this->Session->setFlash('Incorrect username or password.', 'default', array('class' => 'msg error'), 'auth');
    } elseif ($loggedout) {
         $this->Session->setFlash('You have been logged out.', 'default', array('class' => 'msg success'), 'auth');
    }
}

/**
 * Logout action
 */
public function logout() {
    $this->redirect($this->Auth->logout());
}

我不知道出了什么问题。它看起来像一个非常简单的组件使用。我基本上是新的CakePHP。

I have no idea what is going wrong. It seems like a pretty simple component to use. I am new to CakePHP by the way.

基本上,登录应该重定向到/ home,注销应该重定向到/ users / login / 1,您已注销消息再次登录表单。这真的是我需要做的。

basically, login should redirect to /home, and logout should redirect to /users/login/1 so I can display the "you have been logged out" message above the login form again. That is literally all I need to do.

推荐答案

我没有一个Auth组件的经验,怀疑你使这一点比它应该有点困难。文档清楚地表明,默认情况下,Auth将阻止访问除了 login() logout() 。

I don't have a ton of experience with the Auth component, but I suspect you're making this a little more difficult than it should be. The documentation is clear that, by default, Auth will block access to all actions except login() and logout().

我开始删除 userModel loginAction 声明(无论如何,您仍指定了默认值)。此外,您可以删除 login()中的所有代码(保留空函数)。您可以在您的视图中显示来自Auth组件的任何错误消息—将其包含在您的login.ctp中:

I'd start by removing the userModel and loginAction declarations (you're specifying the defaults anyway). Also, you can probably remove all of the code in login() (leave the empty function). You can display any error messages from the Auth compoment in your view—include this in your login.ctp:

echo $this->Session->flash('auth');

请注意,Auth组件将生成类似于您在代码中的消息。

Note that the Auth component will generate messages like the ones you have in your code. It's probably easier just to let it do its thing, and only override that behavior where you really need to.

我也会(暂时)删除 loginRedirect logoutRedirect 声明,看看事情是否如预期的那样运行。 (默认情况下,登录将重定向回您在登录前尝试访问的任何页面。注销将重定向到登录页面,并显示一条闪烁消息,指示您已注销。)如果需要更改默认行为

I would also (temporarily) remove the loginRedirect and logoutRedirect declarations and see if things behave as expected. (By default, logins will redirect back to whatever page you were trying to access before you logged in. Logouts will redirect to the login page, with a flash message indicating you've been logged out.) If you need to change the default behaviors, add them back one at a time and test.

基本上,由于你使用的是Cake默认的用户模型,所以几乎不需要任何配置。这是CakePHP的好处之一。

Basically, since you're using the User model that Cake expects by default, you hardly need any configuration at all—which is one of the nice things about CakePHP.

这篇关于CakePHP认证登录重定向到错误的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆