0

i have overridden my custom controller in etc/di.xml

<preference for="Magento\Catalog\Block\Product\ListProduct" type="Vendor\Module\Block\Catalog\Product\ListProduct" />
<preference for="ToolbarModel" type="Vendor\Module\Block\Catalog\Product\ProductList\Toolbar" />    <!--ToolbarModel: alias for Magento\Catalog\Model\Product\ProductList\Toolbar -->
                    <!--Override for Block catalog Search layer -->
<preference for="Magento\LayeredNavigation\Block\Navigation" type="Vendor\Module\Block\CatalogSearch\Layer" />
                    <!-- Override model: Catalog Resource-->
<preference for="Magento\Catalog\Model\ResourceModel\Product\Collection" type="Vendor\Module\Model\Catalog\Resource\Product\Collection" />

my etc/frontend/di.xml is as

<?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
                    <!--Override controller -catalogsearch-->
<preference for="Magento\CatalogSearch\Controller\Result\Index" type="Vendor\Module\Controller\CatalogSearch\ResultController" />                   

2
  • move your di.xml to etc/frontend
    – Sohel Rana
    Jun 23, 2016 at 10:04
  • Please see updated answer after moving di to etc/frontend...
    – Sushivam
    Jun 23, 2016 at 10:22

1 Answer 1

2

Change your controller code to :


namespace Vendor\Module\Controller\CatalogSearch;

use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Search\Model\QueryFactory;

class ResultController extends \Magento\CatalogSearch\Controller\Result\Index
{

    public function __construct(
        Context $context,
        Session $catalogSession,
        StoreManagerInterface $storeManager,
        QueryFactory $queryFactory,
        Resolver $layerResolver
    ) {
        parent::__construct(
            $context,
            $catalogSession,
            $storeManager,
            $queryFactory,
            $layerResolver
        );
    }
    public function execute()
    {
        echo 'my controller';exit;
    }

}

N.B: You should go with plugin

[Updated]

I am going to build a around plugin example for you.

Put following config to Vendor\Module/etc/frontend/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\CatalogSearch\Controller\Result\Index">
        <plugin name="stackexchange_catalog_search_result" type="Vendor\Module\Controller\CatalogSearch\ResultController" sortOrder="1"/>
    </type>
</config>

Create a plugin class Vendor\Module/Controller/CatalogSearch/ResultController.php


namespace Vendor\Module\Controller\CatalogSearch;

class ResultController
{


}

For more information Click Here

9
  • could yu please help with the plugin code for etc di
    – Sushivam
    Jun 25, 2016 at 15:27
  • Check now only updated answer.
    – Sohel Rana
    Jun 25, 2016 at 15:55
  • Could you explain when and whats the use of around before and afterexecute
    – Sushivam
    Jun 25, 2016 at 16:04
  • If you need some thing before execute then use beforeExecute(), If you need something at the same line of execute call then use aroundExecute(), If you need after execute then use afterExecute(). Clear?
    – Sohel Rana
    Jun 25, 2016 at 16:07
  • i had used as yu mentioned, but even my aroundExecute() is getting called (ie 'my controller' is also printing) and after commenting echo , Magentos core execute() is also getiing called
    – Sushivam
    Jun 25, 2016 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.