5

I am creating extension in Magento2 and also try to follow the all coding standards that magento2 is recommending using EcgM2 standard. Can any one tell me alternative of SERVER['SERVER_NAME'] variable. What i have done is below. I am using SERVER variable alternative in Helper so following is my construct and i have add dependency of $httpRequest

public function __construct(\Magento\Framework\App\RequestInterface $httpRequest,\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Psr\Log\LoggerInterface $logger, \Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this -> scopeConfig = $scopeConfig;
        $this -> storeManager = $storeManager;
        $this -> logger = $logger;
        // MY CUSTOM Request Property 
        $this-> request = $httpRequest;
    }

Further i have used it like below

 $domain =$this->request->getServer('SERVER_NAME');

Is it correct ? Is it better idea?

Can anyone have reference link of documentation that shows the alternative of these global variable and not allowed function in magento2 like

$FILE,base64_decode

9

Ok! I got solution for one or two global variable. I will update question if i found more.

For $_SERVER

public function __construct(\Magento\Framework\App\RequestInterface $httpRequest,\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Psr\Log\LoggerInterface $logger, \Magento\Store\Model\StoreManagerInterface $storeManager)
    {
        $this -> scopeConfig = $scopeConfig;
        $this -> storeManager = $storeManager;
        $this -> logger = $logger;
        // MY CUSTOM Request Property 
        $this-> request = $httpRequest;
    }

Step1: I have added $httpRequest to my __construct and assign to $this_request

Step2: Later we can use that variable like

 $domain =$this->request->getServer('SERVER_NAME');

For $_FILE

public function __construct( \Magento\Framework\Filesystem $filesystem, \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory) {
        $this -> _filesystem = $filesystem;
        // It is required 
        $this -> _fileUploaderFactory = $fileUploaderFactory;

    }

Step1: I have added $fileUploaderFactory to my __construct and assign to $this -> _fileUploaderFactory

Step2: Later we can use that variable like

$uploader = $this -> _fileUploaderFactory -> create(['fileId' => $this -> getAttribute() -> getName()]);

I am using above code in my Model under afterSave method

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.