6

Assume I have a URL like this,

http://mystore.com/cms/sales/order/view/order_id/286/

I want to get values of the order_id which is 286,

Please help with some example or snippet in Magento 2.

asked Oct 3, 2017 at 10:50
0

4 Answers 4

12

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.

answered Oct 3, 2017 at 11:09
1
  • The ObjectManager way worked for me, I was trying to inject from Magento\Backend\App\Action, but injection didn't work so I tried the ObjectManager method, it works, thanks. Commented Oct 3, 2017 at 11:47
4

First, you need to inject \Magento\Framework\App\Request\Http at _constructfunction 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.

answered Oct 3, 2017 at 10:57
1
  • can we use same code in block files? Commented Aug 22, 2019 at 11:15
3

=> 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

answered Oct 3, 2017 at 11:32
2

In a controller which extends Magento\Framework\App\Action\Action you can get the request with $this->getRequest()->getPost().

In a custom class, you need to inject the request in the constructor in below way:

public function __construct(
 \Magento\Framework\App\Request\Http $request,
 ....//rest of parameters here
 ) {
 $this->request = $request;
 ...//rest of constructor here
 }

Then you can get the values like below:

public function getPost()
{
 return $this->request->getParam("order_id");
}
Balwant Singh
1,2902 gold badges12 silver badges29 bronze badges
answered Oct 3, 2017 at 10:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.