Created
May 20, 2020 00:54
-
-
Save DominicWatts/04071f17cd580d5443f8430eaa04d55f 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