How to find Controller, Module, and Action & Route Name in Magento 2

Here are some advantages that you will observe with How to get Controller, Module, and Action & Route Name in Magento 2:

  1. Add the functions into a block of the module
  2. Moduleapp/code/[Name_Spcae]/[Your_Module]/Block/CustomBlock.php

In Magento 2, occasionally you cannot get the real name of the Controller, Module, Action & Router, Magento 2 has various numbers of the Controller, Module, Action & Router, so it is really multifarious to find the real name of the Controller, Module, Action & Router. Most of the time we required to find current controller or action name or module name. To get the path name, block name, controller and activity name from the current URL, from where the control or template files are available.

You can simply get the controller name, action name, router name, and module name in any controller file or class file. This blog shows you how to receive the current block, control name, action name, and path name in Magento 2. Here, in this post how do I try to explain the name of Controller, Module, and Action & Router in the template file or in any class file?

How to acquire Controller, Module, and Action & Route Name in Magento 2?

  • I will attach the functions into a block of the module:

app/code/[Name_Spcae]/[Your_Module]/Block/CustomBlock.php

<?php

namespace [Name_space]\[Your_Module]\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template

{

protected $_request;    

    

public function __construct (

     \Magento\Backend\Block\Template\Context $context,

     \Magento\Framework\App\Request\Http $request,

     array $data = []

) {   

     $this->_request = $request;

     parent::__construct($context, $data);

}

    

public function getControllerModule()

{

     return $this->_request->getControllerModule();

}

    

public function getFullActionName()

{

     return $this->_request->getFullActionName();

}

    

public function getRouteName()

{

     return $this->_request->getRouteName();

}

    

public function getActionName()

{

     return $this->_request->getActionName();

}

    

public function getControllerName()

{

     return $this->_request->getControllerName();

}

    

public function getModuleName()

{

     return $this->_request->getModuleName();

}

}

That’s it. Practice the above mentioned scheme and you can complete how to get the Controller, Module, Action & Route Name in Magento 2. Hope this will be useful, thanks the coding. If you have any queries about how to find the Controller, Module, Action & Route Name process in Magento 2, leave it in the comment section, and I’ll answer as soon as I can.

Last Update: December 20, 2018  

March 17, 2018   369   Nandini R    Operations    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn