1

I am Migrating a website from Magento 1 to Magento 2. In Magento 1, I was using

<?php $isReviewScreen = Mage::app()->getFrontController()->getRequest()->getRouteName() == 'review'; ?>

I am trying to get the equivalent of this code in magento 2? Please help me to do it.

Thanks

  • Please let me know if you have any issue – Rakesh Jesadiya Dec 5 '16 at 5:48
  • can i use <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();?> for this? – Yudi Dec 5 '16 at 5:56
  • @Yudi you can get it directly then why are you using object manager? – Qaisar Satti Dec 5 '16 at 5:58
  • @QaisarSatti i am not getting you properly. Can you please tell me how can i get it directly ? thanks for your quick reply – Yudi Dec 5 '16 at 6:00
  • @Yudi $this->getRequest()->getRouteName(); as it is mention in link answers – Qaisar Satti Dec 5 '16 at 6:05
2

you can get directly in controller

$this->getRequest()->getRouteName();

or phtml file

$this->getRequest()->getRouteName();
0
public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        array $data = []
    ) {
        $this->_request = $request;
        parent::__construct($data);
    }

public function getroute(){
    $this->_request->getRouteName();
 }

You can also get Controller name, action name using below method in template file directly,

$this->getRequest()->getRouteName(),
$this->getRequest()->getControllerName(),
$this->getRequest()->getActionName(),

If you want to get inside Observer file you have to create __construct() method and set RequestInterface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.