Question

I want to override this class function in Magento 2 \Magento\Framework\HTTP\PhpEnvironment\Request::getServer()

etc/module.xml

<module name="Test_Override" setup_version="1.0.0">
    <sequence>
        <module name="Magento_Framework"/>
    </sequence>
</module>

First I tried using a plugin, but it gives me an error every time I run setup:upgrade:

etc/di.xml

<type name="Magento\Framework\HTTP\PhpEnvironment\Request">
    <plugin disabled="false" name="Test_Override_Plugin_Magento_Framework_HTTP_PhpEnvironment_Request" sortOrder="10" type="Test\Override\Plugin\Magento\Framework\Http\PhpEnvironment\Request"/>
</type>

app/code/Test/Override/Plugin/Magento/Framework/Http/PhpEnvironment/Request.php

declare(strict_types=1);

namespace Test\Override\Plugin\Magento\Framework\Http\PhpEnvironment;

class Request
{
    public function beforeGetServer(
        \Magento\Framework\HTTP\PhpEnvironment\Request $subject,
        $name = null,
        $default = null
    ) {
         return [$name,$default];
    }
}

Error when running setup:upgrade :

Cache frontend 'default' is not recognized. As well as defaultcache is not configured

I tried using the preference method, but it is not working at all

etc/di.xml

<preference for="Magento\Framework\HTTP\PhpEnvironment\Request" type="Test\Override\HTTP\PhpEnvironment\Request" />

app/code/Test/Override/HTTP/PhpEnvironment/Request.php

declare(strict_types=1);

namespace Test\Override\HTTP\PhpEnvironment;

class Request extends \Magento\Framework\HTTP\PhpEnvironment\Request
{
    public function getServer($name = NULL,$default= NULL){
      die('testttt');
      return parent::getServer($name,$default);
    }
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top