自定义模块magento中的客户会话为空 [英] customer session is empty in custom module magento

查看:53
本文介绍了自定义模块magento中的客户会话为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了一个使用客户会话的自定义模块,但是奇怪的是,它在实时站点上没有返回客户数据. 我尝试了以下方法:

$sessCustomer = Mage::getSingleton('customer/session', array('name' => 'frontend'));
    echo '<pre>';print_r($sessCustomer->getCustomer()->getData()); echo '</pre>';exit;

它返回:

Array
(
    [website_id] => 1
)

如果我打印客户会话:

Mage::getSingleton('customer/session')->getData();

这将返回(准确的输出):

     array(
[_session_validator_data] => Array
        (
            [remote_addr] => 58.65.183.10
            [http_via] => 
            [http_x_forwarded_for] => 58.65.183.10
            [http_user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0
        )

    [session_hosts] => Array
            (
                [bestevalue.info] => 1
            )
    [messages] => Mage_Core_Model_Message_Collection Object
            (
                [_messages:protected] => Array
                    (
                    )

                [_lastAddedMessage:protected] => 
            )
    [id] => 
)

我在这里很疲惫,已经登录,可以看到带有客户信息的客户仪表板,但是无法在我的自定义模块中使用该会话,请指导我如何解决此问题.

更新:

我已经在/app/etc/local.xml中签入了会话类型为文件

<session_save><![CDATA[files]]></session_save>

那么是否存在使用php提取会话信息的不同方法?我在做什么错了?

更新2:

我也使用了路由器来制作漂亮的网址 public function match(Zend_Controller_Request_Http $request)

在我开始执行此操作后

Mage::getSingleton('core/session', array('name' => 'frontend'));

但仍不能在没有路由器的情况下与路由器一起使用,例如直接访问操作:

site.com/module/controller/action

它可以工作,但不能与路由器一起工作.有什么想法吗? 谢谢,

解决方案

解决方案很简单:

private function _getCustomerSession() {
    Mage::getSingleton('core/session', array('name' => 'frontend'));
    return Mage::getSingleton('customer/session');
}

这将初始化会话并返回它.像这样调用此函数:

$this->_getCustomerSession()->getCustomer()

它将有可用的数据.

更新以供Google员工参考和将来参考:我今天又遇到了这个问题,而且我自己的解决方案不起作用(就像问题一样).进一步的调查显示,我已覆盖了自定义模块中的preDispatch()函数,以检查该模块是否在后端启用,以及是否未重定向至错误页面.在这种情况下,解决方案是-显然-调用parent::preDispatch();

i have made a custom module which uses customer session, but its strange, on live site its not returning customer data. i have tried following methods:

$sessCustomer = Mage::getSingleton('customer/session', array('name' => 'frontend'));
    echo '<pre>';print_r($sessCustomer->getCustomer()->getData()); echo '</pre>';exit;

it returns:

Array
(
    [website_id] => 1
)

if i print the customer session:

Mage::getSingleton('customer/session')->getData();

this returns (exact output):

     array(
[_session_validator_data] => Array
        (
            [remote_addr] => 58.65.183.10
            [http_via] => 
            [http_x_forwarded_for] => 58.65.183.10
            [http_user_agent] => Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:16.0) Gecko/20100101 Firefox/16.0
        )

    [session_hosts] => Array
            (
                [bestevalue.info] => 1
            )
    [messages] => Mage_Core_Model_Message_Collection Object
            (
                [_messages:protected] => Array
                    (
                    )

                [_lastAddedMessage:protected] => 
            )
    [id] => 
)

i am exhausted here , i am logged in, can see the customer dashboard with customer info on it but not been able to use that session in my custom module, please guide me how to fix this.

Update:

i have checked in /app/etc/local.xml that session type is file

<session_save><![CDATA[files]]></session_save>

so is there different method of extracting session info with php ? what am i doing wrong?

Update 2:

i have used router as well to make pretty url public function match(Zend_Controller_Request_Http $request)

on start of this action i placed

Mage::getSingleton('core/session', array('name' => 'frontend'));

but still not working with router without one it is working for example directly accessing the action :

site.com/module/controller/action

it works but not with router. any thoughts? thanks,

解决方案

The solution is simple:

private function _getCustomerSession() {
    Mage::getSingleton('core/session', array('name' => 'frontend'));
    return Mage::getSingleton('customer/session');
}

This initializes the session and returns it. Call this function like this:

$this->_getCustomerSession()->getCustomer()

And it will have the data available.

Update for googlers and future reference: I had this issue again today and my own solution didn't work (like the question). Further investigation revealed I have overridden the preDispatch() function in my custom module to check if the module was enabled in backend and if not redirect to homepage with error. In this case the solution was to -obviously- call parent::preDispatch();

这篇关于自定义模块magento中的客户会话为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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