dsfsdf5646 2015-09-15 02:50
浏览 77


I have created a simple module called product selector. I use Magento collection to retrieve and filter specific products. The data to be used as filter will come from a form post action. As of now this is what I have done so far.

My Controller

public function indexAction() {
        $data['ta'] = $this->getRequest()->getPost('torque-action');
        $data['tr'] = $this->getRequest()->getPost('torque-required');
        $data['tm'] = $this->getRequest()->getPost('torque-metric');

My phtml file

$attr_name = 'product_classification';
$attr_val = $form_data1; //example data only
$metric = $form_data2; //example data only
$metric_val_max = $form_data3; //example data only
$metric_val_min = 1000;

$collection = Mage::getModel('catalog/product')
                ->addFieldTofilter($attr_name, array(
                    'eq' => Mage::getResourceModel('catalog/product')
                ->addFieldTofilter($metric, array('lteq' => $metric_val_max))
                ->addFieldTofilter($metric, array('gteq' => $metric_val_min))

My problem is that, I can't pass the data from controller to phtml template.

I tried to use Mage::getSingleton('core/session')

Mage::getSingleton('core/session')->setSomeSessionVar($data);// In the Controller

$data = Mage::getSingleton('core/session')->getSomeSessionVar(); // In the View;

I successfully retrieve the data using core/session however, when I tried to put it on the collection it goes blank. $form_data1 = echo $data['ta'];

I also use Magento registry but no luck.

Mage::register('some_name', $data); //set on controller

$my_var = Mage::registry('some_name'); //Fetch on View

I successfully get the post form data to controller. I also successfully fetch specific data via Magento collection using fixed values.


How do I pass data from controller to phtml template? What are the best way to do it? Am I going to create a block class?

Thanks in advance.

These are my references http://alanstorm.com/magento_registry_singleton_tutorial https://magento.stackexchange.com/questions/7493/send-data-from-the-controller-to-phtml

  • 写回答

1条回答 默认 最新

  • douzi9744 2015-09-15 03:33


    One of the way to pass data from controller to any block or phtml is to use Magento register.

    Controller: Set or register data using Mage::register('any string name', $data)

    public function indexAction() {
            $data['ta'] = $this->getRequest()->getPost('torque-action');
            $data['tr'] = $this->getRequest()->getPost('torque-required');
            $data['tm'] = $this->getRequest()->getPost('torque-metric');
            Mage::register('t1', $data['ta']);
            Mage::register('t2', $data['tr']);
            Mage::register('t3', $data['tm']);

    Phtml: Get registered data using Mage::registry('your string name')

    $_ta = Mage::registry('t1'); //torque action
    $_tr_max = Mage::registry('t2'); //torque required
    $_tm = Mage::registry('t3'); //torque metric

    Note: This is actually simple to use just beware on your typos.


    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 求苹果推信imessage批量推信技术
    • ¥15 ubuntu 22.04 系统盘空间不足。隐藏的docker空间占用?(相关搜索:移动硬盘|管理系统)
    • ¥15 利用加权最小二乘法求亚马逊各类商品的价格指标?怎么求?
    • ¥15 c++ word自动化,为什么可用接口是空的?
    • ¥15 Matlab计算100000*100000的矩阵运算问题:
    • ¥50 VB6.0如何识别粘连的不规则的数字图片验证码
    • ¥16 需要完整的这份订单所有的代码,可以加钱
    • ¥15 Stata数据分析请教
    • ¥15 请教如何为VS2022搭建 Debug|win32的openCV环境?
    • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料