2 Magento 2で「$ _Server SuperGlobalが検出された直接使用」の修復方法
-
29-09-2020 - |
質問
モジュールの承認に問題があります。そしてMagento Connect検証レポートは次のメッセージを生成します。
$ _SERVER SUPERGLOBALの直接使用
の代替を使用する方法
Magento 2の$ _サーバ['remote_addr']
解決
HELPERクラスを使用する/Magento/Framework/App/Helper/AbstractHelper
クラスを拡張するには、次のようにします。
$this->_remoteAddress
.
AbstractHelper
クラスコンストラクター:
public function __construct(Context $context)
{
$this->_moduleManager = $context->getModuleManager();
$this->_logger = $context->getLogger();
$this->_request = $context->getRequest();
$this->_urlBuilder = $context->getUrlBuilder();
$this->_httpHeader = $context->getHttpHeader();
$this->_eventManager = $context->getEventManager();
$this->_remoteAddress = $context->getRemoteAddress();
$this->_cacheConfig = $context->getCacheConfig();
$this->urlEncoder = $context->getUrlEncoder();
$this->urlDecoder = $context->getUrlDecoder();
$this->scopeConfig = $context->getScopeConfig();
}
.
/Magento/Framework/App/Helper/Context.php
クラスから来ています:
/**
* @return \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
*/
public function getRemoteAddress()
{
return $this->_remoteAddress;
}
.
とそれは\Magento\Framework\HTTP\PhpEnvironment\RemoteAddress
のインスタンスです:
/**
* Retrieve Client Remote Address
*
* @param bool $ipToLong converting IP to long format
* @return string IPv4|long
*/
public function getRemoteAddress($ipToLong = false)
{
if ($this->remoteAddress === null) {
foreach ($this->alternativeHeaders as $var) {
if ($this->request->getServer($var, false)) {
$this->remoteAddress = $this->request->getServer($var);
break;
}
}
if (!$this->remoteAddress) {
$this->remoteAddress = $this->request->getServer('REMOTE_ADDR');
}
}
if (!$this->remoteAddress) {
return false;
}
return $ipToLong ? ip2long($this->remoteAddress) : $this->remoteAddress;
}
. 所属していません magento.stackexchange