【问题标题】:Zend 2 - User session does not workZend 2 - 用户会话不起作用
【发布时间】:2016-01-06 17:08:16
【问题描述】:

我在读取用户模型中记录的用户会话时遇到困难。

当从任何其他模型中检索用户数据时,它可以完美运行。

模块.PHP

'session' => function ($sm) {
                $config = $sm->get('config');
                if (isset($config['session'])) {
                    $session = $config['session']['config']['options']['name'];

                    //Various Session options
                    $manager = new \Zend\Session\SessionManager();                        

                     if(filter_input(INPUT_SERVER, 'APPLICATION_ENV') === 'production'){

                        $manager->getConfig()
                                ->setCookieHttpOnly(true)
                                ->setCookieSecure(false);
                        $manager->start();

                    }

                    return new Session($session);
                }
            },

BaseTable.php

    public function getIdentity($property = null) {
    $storage = $this->getServiceLocator()->get('session');

    if (!$storage) {
        return false;
    }

    $data = $storage->read();

    if ($property && isset($data[$property])) {
        return $data[$property];
    }

    return $data;
}

当我调用 tb_usuario 实例化的 getIdentity 函数时,我收到此错误:

致命错误:在非对象上调用成员函数 get() C:\wamp\www\sigaAvaliacoes\module\application\src\Application\Model\BaseTable.php 在第 73 行

对不起我的英语,谢谢!

【问题讨论】:

    标签: php session zend-framework2


    【解决方案1】:

    对不起,我发现了问题,在 Module.php 中我不是实例 serviceLocator:

    'Usuario' => function($sm) {
                    $tableGateway = new TableGateway('tb_usuario', $sm->get('db_adapter_main'));
                    return new Model\Usuario($tableGateway);
                },
    

    现在开始工作:

    'Usuario' => function($sm) {
                    $tableGateway = new TableGateway('tb_usuario', $sm->get('db_adapter_main'));
                    $updates = new Model\Usuario($tableGateway);
                    $updates->setServiceLocator($sm);
                    return $updates;
                },
    

    谢谢=D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2016-12-02
      相关资源
      最近更新 更多