Skip to main content
<?php

namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(
        Context $context,
        Registry $coreRegistry
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';
        if ($this->registry>_coreRegistry->registry('search_word')) {
            $searchWord = $this->registry>_coreRegistry->registry('search_word');
        }
        //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;
    }
}
<?php

namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(
        Context $context,
        Registry $coreRegistry
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';
        if ($this->registry->registry('search_word')) {
            $searchWord = $this->registry->registry('search_word');
        }
        //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;
    }
}
<?php

namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(
        Context $context,
        Registry $coreRegistry
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';
        if ($this->_coreRegistry->registry('search_word')) {
            $searchWord = $this->_coreRegistry->registry('search_word');
        }
        //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;
    }
}
Corrected format of post.
Source Link
Kishan Savaliya
  • 7.8k
  • 1
  • 13
  • 28
  1. Controller Search.php

    resultPageFactory = $pageFactory; $this->_coreRegistry = $coreRegistry; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } public function execute() { $searchWord = $this->getRequest()->getParam('search'); if ($this->_coreRegistry->registry('search_word')) { $this->_coreRegistry->unregister('search_word'); } // set value to _coreRegistry variable search_word $this->_coreRegistry->register('search_word', $searchWord); $resultPage = $this->resultPageFactory->create(); $result = $this->resultJsonFactory->create(); $block = $resultPage->getLayout() ->createBlock('Vendor\SearchStores\Block\ShowResult') ->setTemplate('Vendor_SearchStores::results_modal.phtml') ->toHtml(); $result->setData(['output' => $block]); return $result; } }
  2. Block ShowResult.php

    _coreRegistry = $coreRegistry; parent::__construct($context); } public function getSearchWord() { $searchWord = ''; if ($this->registry->registry('search_word')) Controller { $searchWord = $this->registry->registry('search_word'); } //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER return $searchWord; } } Search.php
<?php

namespace Vendor\SearchStores\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\JsonFactory;

//use Magento\Framework\Controller\Result\Forward\Factory;

class ReadSearch extends Action
{
    protected $resultPageFactory;

    protected $resultJsonFactory;

    protected $_coreRegistry;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Registry $coreRegistry,
        JsonFactory $resultJsonFactory
    ) {
        $this->resultPageFactory = $pageFactory;
        $this->_coreRegistry = $coreRegistry;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $searchWord = $this->getRequest()->getParam('search');
        if ($this->_coreRegistry->registry('search_word')) {
            $this->_coreRegistry->unregister('search_word');
        }
        // set value to _coreRegistry variable  search_word
        $this->_coreRegistry->register('search_word', $searchWord);
        $resultPage = $this->resultPageFactory->create();
        $result = $this->resultJsonFactory->create();
        $block = $resultPage->getLayout()
            ->createBlock('Vendor\SearchStores\Block\ShowResult')
            ->setTemplate('Vendor_SearchStores::results_modal.phtml')
            ->toHtml();
        $result->setData(['output' => $block]);

        return $result;
    }
}
  1. Block ShowResult.php
<?php

namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(
        Context $context,
        Registry $coreRegistry
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';
        if ($this->registry->registry('search_word')) {
            $searchWord = $this->registry->registry('search_word');
        }
        //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;
    }
}
  1. Controller Search.php

    resultPageFactory = $pageFactory; $this->_coreRegistry = $coreRegistry; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } public function execute() { $searchWord = $this->getRequest()->getParam('search'); if ($this->_coreRegistry->registry('search_word')) { $this->_coreRegistry->unregister('search_word'); } // set value to _coreRegistry variable search_word $this->_coreRegistry->register('search_word', $searchWord); $resultPage = $this->resultPageFactory->create(); $result = $this->resultJsonFactory->create(); $block = $resultPage->getLayout() ->createBlock('Vendor\SearchStores\Block\ShowResult') ->setTemplate('Vendor_SearchStores::results_modal.phtml') ->toHtml(); $result->setData(['output' => $block]); return $result; } }
  2. Block ShowResult.php

    _coreRegistry = $coreRegistry; parent::__construct($context); } public function getSearchWord() { $searchWord = ''; if ($this->registry->registry('search_word')) { $searchWord = $this->registry->registry('search_word'); } //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER return $searchWord; } }
  1. Controller Search.php
<?php

namespace Vendor\SearchStores\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\JsonFactory;

//use Magento\Framework\Controller\Result\Forward\Factory;

class ReadSearch extends Action
{
    protected $resultPageFactory;

    protected $resultJsonFactory;

    protected $_coreRegistry;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Registry $coreRegistry,
        JsonFactory $resultJsonFactory
    ) {
        $this->resultPageFactory = $pageFactory;
        $this->_coreRegistry = $coreRegistry;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $searchWord = $this->getRequest()->getParam('search');
        if ($this->_coreRegistry->registry('search_word')) {
            $this->_coreRegistry->unregister('search_word');
        }
        // set value to _coreRegistry variable  search_word
        $this->_coreRegistry->register('search_word', $searchWord);
        $resultPage = $this->resultPageFactory->create();
        $result = $this->resultJsonFactory->create();
        $block = $resultPage->getLayout()
            ->createBlock('Vendor\SearchStores\Block\ShowResult')
            ->setTemplate('Vendor_SearchStores::results_modal.phtml')
            ->toHtml();
        $result->setData(['output' => $block]);

        return $result;
    }
}
  1. Block ShowResult.php
<?php

namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(
        Context $context,
        Registry $coreRegistry
    ) {
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';
        if ($this->registry->registry('search_word')) {
            $searchWord = $this->registry->registry('search_word');
        }
        //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;
    }
}
  1. Controller

    Controller Search.php

    resultPageFactory = $pageFactory; $this->_coreRegistry = $coreRegistry; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } public function execute() { $searchWord = $this->getRequest()->getParam('search'); if Search.php($this->_coreRegistry->registry('search_word')) { $this->_coreRegistry->unregister('search_word'); } // set value to _coreRegistry variable search_word $this->_coreRegistry->register('search_word', $searchWord); $resultPage = $this->resultPageFactory->create(); $result = $this->resultJsonFactory->create(); $block = $resultPage->getLayout() ->createBlock('Vendor\SearchStores\Block\ShowResult') ->setTemplate('Vendor_SearchStores::results_modal.phtml') ->toHtml(); $result->setData(['output' => $block]); return $result; } }
  2. Block ShowResult.php

    _coreRegistry = $coreRegistry; parent::__construct($context); } public function getSearchWord() { $searchWord = ''; if ($this->registry->registry('search_word')) { $searchWord = $this->registry->registry('search_word'); } //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER return $searchWord; } }
<?php

namespace Vendor\SearchStores\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\JsonFactory;

//use Magento\Framework\Controller\Result\Forward\Factory;

class ReadSearch extends Action
{
    protected $resultPageFactory;

    protected $resultJsonFactory; 

    protected $_coreRegistry;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Registry $coreRegistry,
        JsonFactory $resultJsonFactory
    ){
        $this->resultPageFactory = $pageFactory;
        $this->_coreRegistry = $coreRegistry;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $searchWord = $this->getRequest()->getParam('search');

        if ($this->_coreRegistry->registry('search_word')) {
            $this->_coreRegistry->unregister('search_word');
        }
        // set value to _coreRegistry variable  search_word
        $this->_coreRegistry->register('search_word', $searchWord);

        $resultPage = $this->resultPageFactory->create();

        $result = $this->resultJsonFactory->create();

        $block = $resultPage->getLayout()
            ->createBlock('Vendor\SearchStores\Block\ShowResult')
            ->setTemplate('Vendor_SearchStores::results_modal.phtml')
            ->toHtml();
        $result->setData(['output' => $block]);
        return $result;
    }
}
  1. Block ShowResult.php
<?php
namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(Context $context, Registry $coreRegistry)
    {
       $this->_coreRegistry = $coreRegistry;

        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';

        if ($this->registry->registry('search_word')) {
            $searchWord = $this->registry->registry('search_word');
        }
         //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;

    }
}

After making the above changes in your Controller and Block file, you can get a search word in your template file.

  1. Controller Search.php
<?php

namespace Vendor\SearchStores\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\Registry;
use Magento\Framework\Controller\Result\JsonFactory;

//use Magento\Framework\Controller\Result\Forward\Factory;

class ReadSearch extends Action
{
    protected $resultPageFactory;

    protected $resultJsonFactory; 

    protected $_coreRegistry;

    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        Registry $coreRegistry,
        JsonFactory $resultJsonFactory
    ){
        $this->resultPageFactory = $pageFactory;
        $this->_coreRegistry = $coreRegistry;
        $this->resultJsonFactory = $resultJsonFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $searchWord = $this->getRequest()->getParam('search');

        if ($this->_coreRegistry->registry('search_word')) {
            $this->_coreRegistry->unregister('search_word');
        }
        // set value to _coreRegistry variable  search_word
        $this->_coreRegistry->register('search_word', $searchWord);

        $resultPage = $this->resultPageFactory->create();

        $result = $this->resultJsonFactory->create();

        $block = $resultPage->getLayout()
            ->createBlock('Vendor\SearchStores\Block\ShowResult')
            ->setTemplate('Vendor_SearchStores::results_modal.phtml')
            ->toHtml();
        $result->setData(['output' => $block]);
        return $result;
    }
}
  1. Block ShowResult.php
<?php
namespace Vendor\SearchStores\Block;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;
use Magento\Framework\Registry;

class ShowResult extends Template
{
    protected $_coreRegistry;

    public function __construct(Context $context, Registry $coreRegistry)
    {
       $this->_coreRegistry = $coreRegistry;

        parent::__construct($context);
    }

    public function getSearchWord()
    {
        $searchWord = '';

        if ($this->registry->registry('search_word')) {
            $searchWord = $this->registry->registry('search_word');
        }
         //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER
        return $searchWord;

    }
}

After making above changes in your Controller and Block file, you can get search word in your template file.

  1. Controller Search.php

    resultPageFactory = $pageFactory; $this->_coreRegistry = $coreRegistry; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } public function execute() { $searchWord = $this->getRequest()->getParam('search'); if ($this->_coreRegistry->registry('search_word')) { $this->_coreRegistry->unregister('search_word'); } // set value to _coreRegistry variable search_word $this->_coreRegistry->register('search_word', $searchWord); $resultPage = $this->resultPageFactory->create(); $result = $this->resultJsonFactory->create(); $block = $resultPage->getLayout() ->createBlock('Vendor\SearchStores\Block\ShowResult') ->setTemplate('Vendor_SearchStores::results_modal.phtml') ->toHtml(); $result->setData(['output' => $block]); return $result; } }
  2. Block ShowResult.php

    _coreRegistry = $coreRegistry; parent::__construct($context); } public function getSearchWord() { $searchWord = ''; if ($this->registry->registry('search_word')) { $searchWord = $this->registry->registry('search_word'); } //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER return $searchWord; } }

After making the above changes in your Controller and Block file, you can get a search word in your template file.

Source Link
Kishan Savaliya
  • 7.8k
  • 1
  • 13
  • 28
Loading