Forked from DominicWatts/Magento-2-Superglobals-Get-PostParameters-Session
Created
November 22, 2021 21:11
-
-
Save DomPixie/d05101d654facc57cfde15e60d222e25 to your computer and use it in GitHub Desktop.
Magento 2 Superglobals Get & Post Parameters + Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Controller | |
*/ | |
// $_SERVER | |
$this->getRequest()->getServer('SERVER_NAME'); | |
// $_GET | |
$this->getRequest()->getParams(); | |
$this->getRequest()->getParam('getparam'); | |
// $_POST | |
$this->getRequest()->getPostValue(); | |
$this->getRequest()->getPostValue('param'); | |
/* | |
* Outside Controller | |
*/ | |
protected $request; | |
public function __construct( \Magento\Framework\App\Request\Http $request) { | |
$this->request = $request | |
} | |
// $_SERVER | |
$this->request->getServer('SERVER_NAME'); | |
// $_GET | |
$this->request->getParams(); | |
$this->request->getParam('getparam') | |
// $_POST | |
$this->request->getPostValue(); | |
// pass to block (I think) | |
public function getRequest() { | |
return $this->request; | |
} | |
// template | |
$this->getRequest()->getParams(); // etc | |
// $_SESSION | |
// Different sessions: | |
// - \Magento\Backend\Model\Session | |
// - \Magento\Catalog\Model\Session | |
// - \Magento\Checkout\Model\Session | |
// - \Magento\Customer\Model\Session | |
// - \Magento\Newsletter\Model\Session | |
// for example | |
protected $catalogSession; | |
public function __construct(\Magento\Catalog\Model\Session $catalogSession){ | |
$this->catalogSession = $catalogSession; | |
} | |
$this->catalogSession->setMyValue('test'); | |
$this->catalogSession->getMyValue(); | |
// pass to block | |
public function getCatalogSession() { | |
return $this->catalogSession; | |
} | |
// template | |
$this->getCatalogSession()->setMyValue('test'); | |
$this->getCatalogSession()->getMyValue(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment