1

I am trying to create a plugin for Magento\Framework\App\Request\Http class for setPathInfo(). However I am unable to do so. I am getting error

[InvalidArgumentException] There are no commands defined in the "cache" namespace

However I am able to create plugin for other classes. There is no error in module, rest of the code is working fine.

My 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\Framework\App\Request\Http">
    <plugin name="testExample" type="\Vendor\PluginExample\Plugin\TestExample"/>
</type>
</config>

My plugin file -

namespace Vendor\PluginExample\Plugin;

class TestExample {

protected $logger;

public function __construct(
\Psr\Log\LoggerInterface $loggerInterface
) {
    $this->logger = $loggerInterface;
}

public function afterSetPathInfo(Magento\Framework\App\Request\Http $pathInfo = null) {
    $this->logger->debug("works);

}

}

What am i doing wrong?? Can't we create a plugin for Framework classes ?

5
  • Definately, You can create a plugin for Framework class. have you check using remove var/generation folder? Jul 21, 2017 at 7:18
  • To fix this error [InvalidArgumentException] There are no commands defined in the "cache" namespace, you can simply execute bin/magento command and the errors are displayed in bottom of the commands. Mostly, if you clear var/di, var/generation, var/cache & var/page_cache, this error will be gone.
    – Mike
    Jul 21, 2017 at 9:46
  • @Rakesh , yes I have already tried to remove var/generation folder , but it didn't help. Can you confirm if the above code is correct ?
    – Neha.Soni
    Jul 21, 2017 at 12:29
  • @Mike , I tried fixing the issue but it did'nt help
    – Neha.Soni
    Jul 21, 2017 at 12:30
  • Appears this class can't have plugins, see github.com/magento/magento2/issues/11840
    – Mondane
    May 8, 2019 at 14:55

1 Answer 1

0

It looks like your plugin-type class reference has an additional forward slash.

<plugin name="testExample" type="\Vendor\PluginExample\Plugin\TestExample"/>

I believe this one should be work. FYI: Didn't test the code

<?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\Framework\App\Request\Http">
    <plugin name="testExample" type="Vendor\PluginExample\Plugin\TestExample"/>
  </type>
</config>

Clear the var/generation and try it out in Developer mode.

Additional info:

This Magento\Framework\App\Request\Http is the HTTP entry point in the pub/index.php Magento\Framework\App\Bootstrap class. So it might be possible that we couldn't use this in a plugin but worth giving it a try.

https://devdocs.magento.com/guides/v2.4/config-guide/bootstrap/magento-bootstrap.html

Cheers!

Your Answer

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

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