Magento2: Cannot override `Http Request` class

Created on 29 Oct 2017  路  7Comments  路  Source: magento/magento2


Preconditions


  1. Magento 2.1.9
  2. PHP 7.0.22

Steps to reproduce

1. Create new simple module (registration.php, composer.json, module.xml)

  • ##### registration.php

\Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Mehran_UnicodeURL', __DIR__ );

  • ##### composer.json
    { "name": "mehran/module-unicode-url", "description": "Magento 2 Allow Unicode URL", "type": "magento2-module", "version": "1.0.0", "license": [ "OSL-3.0", "AFL-3.0" ], "autoload": { "files": [ "registration.php" ], "psr-4": { "Mehran\\UnicodeURL\\": "" } } }
  • ##### module.xml
    <?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Mehran_UnicodeURL" setup_version="0.0.1"/> </config>

_Until now, Everything is fine, and setup:upgrade run as well._

2. Add di.xml, Request.php for overriding getRequestUri() method of Http Request class.

  • ##### 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\HTTP\PhpEnvironment\Request"> <plugin name="mehran-request" type="Mehran\UnicodeURL\Plugin\Request" /> </type> </config>
  • ##### Plugin\Request.php
    namespace Mehran\UnicodeURL\Plugin; class Request { public function beforeGetRequestUri(\Magento\Framework\HTTP\PhpEnvironment\Request $req) { die('Please Die:('); } }

    3. run command: php bin/magento setup upgrade -vvv

Expected result

  1. No error, and upgrade successfully.

Actual result

  1. This error happend:
    [InvalidArgumentException]
    Cache frontend 'default' is not recognized.

cannot-override-request-class

Clear Description Format is valid non-issue

Most helpful comment

@Mehran91z, thank you for your report.
We've created internal ticket(s) MAGETWO-84072 to track progress on the issue.

All 7 comments

Can you reproduce an issue using a supported operating system: http://devdocs.magento.com/guides/v2.2/install-gde/system-requirements-tech.html? Note that Windows is not in the list.

OK, I need to install an Ubuntu then. I'll give it a try. But I doubt that the OS causing that!
Let me check it out.

I install Ubuntu 16.04 with Xampp 7.0.24 but the _problem still exists_:

magento_class_override_error

@Mehran91z, thank you for your report.
We've created internal ticket(s) MAGETWO-84072 to track progress on the issue.

Hello,
can anyone test this (2.2.4)?:

<type name="Magento\Framework\Cache\InvalidateLogger">
    <arguments>
        <argument name="request" xsi:type="object">Magento\Framework\App\Request\Http\Proxy</argument>
    </arguments>
</type>

Making "request" parameter for Magento\Framework\Cache\InvalidateLogger constructor lazy (Proxy) seems to resolve the issue in our setup.

Edit: By https://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html :

Limitations
Plugins cannot be used with any of the following:
Objects that are instantiated before Magento\Framework\Interception is bootstrapped

It seem this is the case with Magento\FrameworkApp\Request\Http:

Magento\Framework\Cache\InvalidateLogger::execute() is called (only for logging purpose) before Magento\Framework\Interception is bootstrapped:

    public function execute($invalidateInfo)
    {
        $this->logger->debug('cache_invalidate: ', $this->makeParams($invalidateInfo));
    }

    private function makeParams($invalidateInfo)
    {
        $method = $this->request->getMethod();
        $url = $this->request->getUriString();
        return compact('method', 'url', 'invalidateInfo');
    }

which requires fully featured Magento\FrameworkApp\Request\Http class (despite being \Proxy'ied).

Commenting body of Magento\Framework\Cache\InvalidateLogger::execute() allows one to make Request class pluginable.

@pczerkas I don't think your proxy solution is working. After adding it, the compile fails with

There are no commands defined in the "cache" namespace.

Tested on 2.3.1

Thanks @pczerkas for your investigation!

Was this page helpful?
0 / 5 - 0 ratings