【问题标题】:CakePHP Auth redirect method redirects me to /users/img/favicon.png?CakePHP Auth 重定向方法将我重定向到 /users/img/favicon.png?
【发布时间】:2012-09-25 08:44:40
【问题描述】:

我正在尝试在 CakePHP 中使用 AuthComponent 的默认行为,但是当我尝试在 login 操作中使用方法 $this->Auth->redirect() 时,我得到了一个奇怪的行为。

我想将登录的用户重定向到 view,他之前从那里被重定向,例如 CakePHP log in redirect to requested URL 文章。我也读过CakePHP 1.3Authentication,但不明白我错在哪里。

这是我的代码:

应用控制器

public $components = array(
    'Session',
    'Auth' => array(
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authError' => 'Error message',
        'authorize' => array(
            'Controller',
            'Actions' => array(
                'actionPath' => 'controllers'
            )
        ),
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

用户控制器

public function login () {
    if ($this->request->is ('post')){
        if ($this->Auth->login()) {
            $auth_user = $this->Auth->user();
            $this->User->id = $auth_user['id'];
            $this->User->saveField('last_login', date('Y-m-d H:i:s'));

            $this->Session->write('flash_element','done');
            $this->Session->setFlash('Welcome back <b>'.$auth_user['username'].'</b>!');

            $this->redirect ($this->Auth->redirect());
        } else {
            $this->set('flash_element','warning');
            $this->Session->setFlash('Wrong login');
        }
    }
}

此设置将我重定向到http://site.com/users/img/favicon.png
如果我 debug $this-&gt;Auth-&gt;redirect() 我得到准确的 /users/img/favicon.png 字符串。

我尝试在 default.ctp 布局文件中注释一行,然后在 everything works perfect 之后添加注释,为什么?

<!doctype html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title><?php echo $title_for_layout; ?></title>

        <meta name="HandheldFriendly" content="True">
        <meta name="MobileOptimized" content="320">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <?php
        echo $this->Html->css('style.css');
        //echo $this->Html->meta('icon','img/favicon.png');
        ...

我不明白为什么这一行会破坏我的AuthComponent,是网站图标代码错误吗?

应用程序的其余部分与所有模型、视图和控制器完美配合。

【问题讨论】:

  • 你试过在不使用 Cake 的 Html Helper 的情况下输出 favicon 吗?像这样: 你仍然得到相同的结果吗?

标签: php authentication redirect cakephp-2.1


【解决方案1】:

尝试从用户控制器中移除脚手架并烘焙它。

【讨论】:

    【解决方案2】:

    尝试将“loginRedirect”键放入您的 AuthComponent 设置中:

    public $components = array(
        'Session',
        'Auth' => array(
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'loginRedirect' => array('controller' => 'users', 'action => 'view'),
            'authError' => 'Error message',
            'authorize' => array(
                'Controller',
                'Actions' => array(
                    'actionPath' => 'controllers'
                )
            ),
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email')
                )
            )
        )
    );
    

    $this-&gt;Auth-&gt;login() 也应该自动神奇地重定向你。

    【讨论】:

    • 我已经尝试过没有改变问题,我真的不知道我做了什么来解决这个问题!
    • 您是否在 Web 服务器日志中发现了某种错误? $this-&gt;redirect ($this-&gt;Auth-&gt;redirect()); 在 $this->redirect 和 () 之间有一个空格。还要检查您所在的控制器 app_controller 等文件的开头或结尾是否有空格。
    猜你喜欢
    • 2020-07-04
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 2014-05-23
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多