2

I am trying to override product controller. Here is the code...

app/code/Test/Local/etc/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">    
    <preference for="Magento\Catalog\Controller\Product\View" type="Test\Local\Controller\Catalog\Product\View" />
</config>

app/code/Test/Local/Controller/Product/view.php

<?php

namespace Test\Local\Controller\Catalog\Product;

class View extends \Magento\Catalog\Controller\Product\View
{    
    /**
     * Product view action
     *
     * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {

        // logging to test override    
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Controller Override Test');

        echo "In"; exit;
    }    
}
?>

I am not getting what is going wrong, but this is not working somehow.

9
  • have you got any error?
    – Bojjaiah
    Feb 13 '17 at 10:08
  • No there is no error.
    – Hardik
    Feb 13 '17 at 10:11
  • have you ran upgrade and clean command?
    – Bojjaiah
    Feb 13 '17 at 10:13
  • Yes already ran that commands
    – Hardik
    Feb 13 '17 at 10:14
  • I ran the your code. it's working fine. re-check your module or ran the upgrade and clean commands?
    – Bojjaiah
    Feb 13 '17 at 10:30
4

let's try this.

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'ABCCompany_BranchOne',
    __DIR__
);

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="ABCCompany_BranchOne" schema_version="1.0.0" setup_version="1.0.0" />
</config>

etc/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">
    <preference for="Magento\Catalog\Controller\Product\View" type="ABCCompany\BranchOne\Controller\Index\View" />
</config>

Controller/View.php

<?php

namespace ABCCompany\BranchOne\Controller\Index;

class View extends \Magento\Catalog\Controller\Product\View
{

    /**
     * Product view action
     *
     * @return \Magento\Framework\Controller\Result\Forward|\Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        // logging to test override
        $logger = \Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface');
        $logger->debug('Controller Override Test');
        echo "In"; exit;
    }
}
?>

after created and pasted above code, please run the below commands.

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy

php bin/magento setup:clean

let me know if you have any issue.

5
  • I am getting this error. "There are no commands defined in the “setup” namespace" when trying this command "php bin/magento setup:upgrade"
    – Hardik
    Feb 13 '17 at 11:11
  • where you are tried this command? specify the path. and also have you installed composer in your system?
    – Bojjaiah
    Feb 13 '17 at 11:17
  • This command working fine before adding above code. I am trying in my local server in root of my magento installation.
    – Hardik
    Feb 13 '17 at 11:19
  • You have to install composer and run composer update command Feb 13 '17 at 11:22
  • @Hardik have you updated?
    – Bojjaiah
    Feb 13 '17 at 12:02
1

Change your namespace from:

namespace Csoft\Local\Controller\Catalog\Product;

To:

namespace Test\Local\Controller\Catalog\Product;
1
  • Please check updated question...
    – Hardik
    Feb 13 '17 at 9:43
0

Your controller file path is wrong

app/code/Test/Local/Controller/Product/view.php

It should be

app/code/Test/Local/Controller/Catalog/Product/view.php

You miss catalog folder

3
  • Thanks for your ans. I have tried from this solution. stackoverflow.com/a/39424354
    – Hardik
    Feb 13 '17 at 9:52
  • Yes in this solution have also mistake. Becuase you have define file in di.xml is Test\Local\Controller\Catalog\Product\View so your controller file path same as above answer Feb 13 '17 at 9:54
  • Tried this but not working. :(
    – Hardik
    Feb 13 '17 at 10:23
0

check your file name

app/code/Test/Local/Controller/Product/view.php

it should be View.php

Note the capital V. as class name and file name must be same to locate the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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