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条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 使用DWY100k数据集对UEA进行测试,出现报错:IndexError: index 125000 is out of bounds for axis 0 with size 95500
      • ¥15 前端vue实现根据图片url生成pdf文件
      • ¥15 RfidReader资源Q个
      • ¥20 user-agent是否是唯一的,有没有可能相同
      • ¥15 关于#开会#的问题,如何解决?(语言-c++)
      • ¥15 关于#二十四点问题#的问题
      • ¥15 运行kitex的demon出错(求大家解决)
      • ¥15 开发一个类似 typora 这样的软件需要多少钱?
      • ¥15 clearcase7.0安装包
      • ¥15 断点回归模型月度核密度检验不连续