session - Cake PHP 2.x Auth 数据仅在一个 Controller 中可用,而在其余 Controller 中为 null。知道为什么吗?

标签 session cakephp login controller authorization

我是 CakePHP 的新手,尽管搜索了高低,但我无法找到看似微不足道的问题的答案。问题是,通过我的 UserController 登录后,我丢失了在 $this->Auth->user() 中找到的数据。在所有其他 Controller 上。任何与 UserController 无关的 View 将始终具有空数据和 $this->Auth->loggedIn()将返回假。

在我的 AppController 类中,我试图将数据保存到 beforeFilter() 中的一个变量中。这样我就可以在我的 View 中检查变量,但它没有什么区别。这是我的 UserController 和 AppController 类的相关代码:

class AppController extends Controller {

// Pass settings in $components array
public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'home', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'home', 'action' => 'index')
    )
);

public function beforeFilter() {
    //debug($this->Auth->user());
    $this->set('loggedIn', $this->Auth->user());    

    }

}

用户类:
App::uses('AppController', 'Controller');

class UsersController extends AppController {

public $helpers = array('Html', 'Form');
public $name = 'Users';

public function beforeFilter() {
    parent::beforeFilter();
}

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

public function logout() {
    return $this->redirect($this->Auth->logout());
}

}   

我也确保使用 parent::beforeFilter()在我的其他 Controller 中。

最佳答案

我相信我找到了解决方案。在我的 HomeController 中,这是我登录后一直查看的页面,在关闭 php 声明之后似乎有一些不可见的字符 ?> (也不需要)所以我删除了它和 ?>瞧,一切都按预期进行。

在我尝试手动使用 session_start() 后,有人提示我出了点问题。在我的布局中,我收到了关于 ?> 之后的行的错误在 HomeController 类中。

关于session - Cake PHP 2.x Auth 数据仅在一个 Controller 中可用,而在其余 Controller 中为 null。知道为什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14509742/

相关文章:

php - session 不起作用

java - 仅修改当前请求的SecurityContext

login - 自定义身份验证 - 登录 Symfony2 消息

php - session 变量在 echo 中工作但在 SQL 查询中不起作用

php - PphpMyAdmin - 错误无法在没有错误的情况下启动 session : How to fix?

cakephp 2 使用主题标签重定向 url

php - Cakephp:模型中属于多个级别

php - Cakephp + 用于 PHP 的 Visual Studio 工具

login - 如何通过代码将登录项添加到山狮 osx

php - 站点安全正则表达式处理程序? (PHP)