Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DomPixie/d05101d654facc57cfde15e60d222e25 to your computer and use it in GitHub Desktop.
Save DomPixie/d05101d654facc57cfde15e60d222e25 to your computer and use it in GitHub Desktop.
Magento 2 Superglobals Get & Post Parameters + Session
<?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