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() {
        $this->loadLayout();
        $data['ta'] = $this->getRequest()->getPost('torque-action');
        $data['tr'] = $this->getRequest()->getPost('torque-required');
        $data['tm'] = $this->getRequest()->getPost('torque-metric');
        $this->renderLayout();
    }

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')
                ->getCollection()
                ->addFieldTofilter($attr_name, array(
                    'eq' => Mage::getResourceModel('catalog/product')
                                    ->getAttribute($attr_name)
                                    ->getSource()
                                    ->getOptionId($attr_val)
                    ))
                ->addFieldTofilter($metric, array('lteq' => $metric_val_max))
                ->addFieldTofilter($metric, array('gteq' => $metric_val_min))
                ->addAttributeToSelect('*')
                ->load();

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.

QUESTIONS:

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
    关注

    Awesome!

    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() {
    
            $this->loadLayout();
    
            $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']);
    
            $this->renderLayout();
        }
    

    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.

    Thanks!

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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示