Cakephp Auth组件-主页重定向循环



我想在主页上有一个登录表单,注册的用户应该重定向到users/index

使用以下代码,我的主页将转到重定向循环
有人能告诉我问题出在哪里吗??

注意:-事实上,如果我将行更改为,它就可以完美工作

$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');

应用程序控制器

  public function beforeFilter(){
            $this->Auth->autoRedirect = false;
            $this->Auth->loginAction = array('controller' => './', 'action' => 'index');
            $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
            $this->Auth->logoutRedirect = array('controller' => './', 'action' => './');
            $this->Auth->authorize = 'controller';
            $this->Auth->authError= 'You need permissions to access this page';
            $this->Auth->allow('index');
            $this->set('Auth',$this->Auth);
        }

用户控制器

public function login(){
        $id = $this->Auth->user('id');
        if(empty($id)){
            if($this->request->is('post')){
                if($this->Auth->login()){
                    $this->redirect($this->Auth->redirect());   
                }else{
                    $this->Session->setFlash('Invalid Username or password');
                }
            }
        }else{
            $this->redirect(array('action'=>'index'));
        }   
    }

谢谢你的帮助。。。

您在这里几乎回答了自己的问题:

注意:-事实上,如果我将行更改为,它就可以完美工作

$this->Auth->loginAction=array('controller'=>'users','action'=>'login');

事实上,这是可行的,这就是它应该有的样子。现在,您正在告诉auth组件您的loginAction(保存登录逻辑的操作)是./控制器的index操作(它甚至不存在)。我假设您将其与loginRedirect变量混淆,后者用于设置成功身份验证后要转到的页面。

如果你只想让注册用户访问你的网站,你可以有这样的东西。。。至少,这是我在我的网站上实现类似功能的方式。。。

在app_controller文件中,将以下内容添加到beforeFilter()函数的开头

function beforeFilter(){
    //Check if user was able to log in thru Auth using your form in the homepage
    if($this->isLoggedIn() == TRUE){
        $this->layout = 'default'
    }else{
        // You can created this layout with a login form and 
        // whatever else you need except <?php echo $content_for_layout; ?>
        // Any registered user will be allowed to login using the form
        // and continue on to your site using the default layout
        // But it guarantees no one else can see your default site
        $this->layout = "unregistered_user"
    }
}

在App_controller.php上,您可以创建此函数

function isLoggedIn(){
    // You can also use $this->Auth->user directly in your App's beforeFilter()
    // But I just like to have functions so I can reuse
    if($this->Auth->user()){
       $loggedin= TRUE;
    }else{
        $loggedin= FALSE;
    }
    return $loggedin;
}

我有一些类似于我的网站,但只在维护模式下使用。我仍在开发我的网站。我用这种方式看到的唯一问题是,我的错误没有发送到我想要的布局,我还没有时间/需要查看。假设用户键入http://www.mydomain.com/inexistentpage然后cake将它们转移到我的默认布局。这可能很容易解决,但我还没有时间去做。

注意:我很快就想到了这一点,正因为如此,这个代码还没有经过测试。但是,如果你有任何问题,请让我知道,我会测试它并发布回来。

在视图中使用$this->requestAction(anotherController/action);可能会调用另一个控制器->操作。您必须确保另一个控制器->操作具有正确的权限。否则你会得到重定向循环。

通过在beforeFilter()回调中将$this->auth->allow('action name');添加到另一个控制器页面来解决此问题。

最新更新