home

Search A2Z 24

Magento 2: Get Controller, Module, Action & Route Name

X

This post shows, you will see How to get Controller, Module, Action & Route Name in Magento 2:

This post shows, you will see How to get Controller, Module, Action & Route Name in Magento 2:

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

In Magento 2, sometimes you can not get the actual name of the Controller, Module, Action & Router, Magento 2 has many numbers of the Controller, Module, Action & Router, so it is very difficult to find the actual name of the Controller, Module, Action & Router.
Many time we required to get current controller or action name or module name.
To get route name, module name, controller and action name for magento from current URL, anywhere in controller or even with template files.
You might easily get controller name, action name, router name and module name in any template file or class file.

This post shows us that how we can get name of the current module, controller name, action name and route name in Magento 2.

Using Dependency Injection (DI)

Below is a block class of my custom module (Satz24_Custom). I have injected object of \Magento\Framework\App\Request\Http class in the constructor of my module's block class.

app/code/Satz24/Custom/Block/CustomBlock.php

namespace Satz24\Custom\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();
    }
}

See more functions in vendor/magento/framework/App/Request/Http.php

Now, we use can the function in template (.phtml) file.

Get Route Name :

echo $block->getRouteName();

Get Module Name :

echo $block->getModuleName();

Get Controller Name :

echo $block->getControllerName();

Get Action Name :

echo $block->getActionName();

Get Full Action Name :

echo $block->getFullActionName();

Get Controller Module :

echo $block->getControllerModule();

Using Object Manager

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();		
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
echo $request->getRouteName();
echo $request->getModuleName();
echo $request->getControllerName();
echo $request->getActionName();
echo $request->getFullActionName();
echo $request->getControllerModule();

About Author

by Admin
loading...

Share your thoughts!

Login as a member to access comment posting block !! click-here

Thoughts From Other Users (0)

No Comments

×