1

I am submitting my form data to controller and getting filtered product collection using helper function.But Now I want that result data (from execute()) in my phtml file.Then I can display those filtered products.

Note : No need that data directly from controller to phtml, We can also use block or helper but the result collection I need in phtml file.

Can you help me on this?

2 Answers 2

2

You can create function in block example as below:

public function getEvents(){ //get values of current page $page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1; //get values of current limit $pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 5; $collection = $this->_eventCollectionFactory->create(); $collection->addFieldToFilter('is_active',1); $collection->setPageSize($pageSize); $collection->setCurPage($page); $collection->getSelect()->order('main_table.start_time asc'); return $collection; } 

Using below code you can call this function in phtml file

$block->getEvents(); 

EDIT: How to get post data in block. you need to add below code in block

protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, .... ) { $this->request = $request; } public function getPostData() { // you can use below code in any function $postData = $this->getRequest()->getParams(); } 
11
  • How can we get form data in block ? If it is controller then we can give the controller name in form action but if it is block how ? Can you tell me the process @Prashant Commented Nov 24, 2016 at 5:28
  • you need to give controller action name in form action add above updated code in block and you will get post data in block Commented Nov 24, 2016 at 5:39
  • Hi @Prashant, As you said i just put my controller path in form action.In controller execute function i did not write anything related to this.And I put your edited code in one block file and called that (block) function from my phtml file (which is having form).After that I am getting this error [Zend\Stdlib\Parameters Object ( [storage:ArrayObject:private] => Array ( ) ) ]. what is the problem and how to get post data in block.Can help me. Commented Nov 24, 2016 at 6:18
  • update code with code $this->getRequest()->getParams() please check and use it Commented Nov 24, 2016 at 6:23
  • After Update Your code i am getting this result (Array ( [id] => 21 )).I am not getting my form post data.Do I need to change anything ? Commented Nov 24, 2016 at 6:35
1

For sending data to controller to phtml you can use registry

Controller

protected $_coreRegistry = null; public function __construct( .... \Magento\Framework\Registry $_coreRegistry ) { ..... $this->_coreRegistry = $_coreRegistry; .... } public function execute() { $postModel = $this->_modelPostFactory->create(); // Load the item with ID is 1 $item = $postModel->load(1); // var_dump($item->getData()); // Get news collection $postCollection = $postModel->getCollection(); // Load all data of collection var_dump($postCollection->getData()); $this->_coreRegistry->register('data_test', $postCollection); } 

in your block

 protected $_coreRegistry = null; public function __construct( .... \Magento\Framework\Registry $_coreRegistry ) { ..... $this->_coreRegistry = $registry; .... } public function getTest() { $postCollection = $this->_coreRegistry->registry('data_test'); } 

in phtml

$block->getTest(); 
10
  • Hi @Fme I tried with your code but it is giving this error (Uncaught TypeError: Argument 2 passed to vendor name\module name\Controller\Index\View::__construct() must be an instance of Magento\Framework\Registry, none given) I am using this (\Magento\Framework\Registry $_coreRegistry) in controller constructor.How can I resolve this. Commented Nov 24, 2016 at 9:15
  • did you remove the var/generation folder. and can you share your latest so i can look at that Commented Nov 24, 2016 at 9:22
  • namespace vendor\module\Controller\Index;class View extends \Magento\Framework\App\Action\Action { protected $_coreRegistry = null; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Registry $_coreRegistry ) { $this->_coreRegistry = $_coreRegistry; parent::__construct($context); } public function execute() { //getting here collection $productCollection = $this->_objectManager->create('vendor\module\Helper\Data')->getProductCollection($data); $this->_coreRegistry->register('data_test', $productCollection->getData()); Commented Nov 24, 2016 at 9:43
  • namespace vendor\module\Block; class FilteredCollection extends \Magento\Framework\View\Element\Template { protected $_coreRegistry = null; public function __construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $_coreRegistry, array $data = array() ) { $this->_coreRegistry = $_coreRegistry; parent::__construct($context, $data); } public function getTest() { $postCollection = $this->_coreRegistry->registry('data_test'); return $postCollection; } } Commented Nov 24, 2016 at 9:45
  • did you remove var/generation and var/cache folder? Commented Nov 24, 2016 at 9:47

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.