Get parameters from URL Magento 2
By ObjectManager:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Request\Http');
echo $param = $request->getParam('order_id');
By Factory Method
protected $request;
public function __construct(
...
\Magento\Framework\App\Request\Http $request,
...
) {
$this->request = $request;
}
$this->request->getParam('order_id');
Note: Do not use objectManager directly in files as Magento 2 coding standards.
First, you need to inject \Magento\Framework\App\Request\Http
at _construct
function then using
protected $request;
public function __construct(
\Magento\Framework\App\Request\Http $request,
....//rest of parameters here
) {
$this->request = $request;
...//rest of constructor here
}
public function getIddata()
{
// use
$this->request->getParams(); // all params
return $this->request->getParam('order_id');
}
At BLock class, you don't need to inject this class. Use
$this->getRequest()
instead of $this->request
.
=> Factory Method :
<?php
namespace Namespace\Module\Something;
class ClassName
{
protected $request;
public function __construct(
\Magento\Framework\App\Request\Http $request,
....//rest of parameters here
) {
$this->request = $request;
...//rest of constructor here
}
public function getPost()
{
return $this->request->getParam("order_id");
}
}
=> Object Manager :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('Magento\Framework\App\Request\Http');
echo $request->getParam('order_id');
=> Note : Do not use direct object manager as magento coding format