Magento 2 - Get Controller name, Module name, Action name and Router name

Use request class of Magento 2 for getting Controller name, Module name, Action name and Router name:


    protected $request;
    public function __construct(
    ...
        \Magento\Framework\App\Request\Http $request
        ...
    ){
    ...
        $this->request = $request;
        ...
    }
    public function YourFunction()
    {
        echo "Module Name = " . $this->request->getModuleName();
        echo "Controller Name = " . $this->request->getControllerName();
        echo "Action Name = " . $this->request->getActionName();
        echo "Route Name = " . $this->request->getRouteName();
    }

Comments