Magento 2: Jak uzyskać nazwę kontrolera, modułu, akcji i routera?


Odpowiedzi:


33

Użyj poniższego kodu w klasie kontrolera, aby uzyskać nazwę kontrolera, modułu, akcji i trasy:

<?php
    namespace Custom\Module\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $request;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Request\Http $request
    ){
        parent::__construct($context);
        $this->request = $request;
    }

    public function execute()
    {
        $moduleName = $this->request->getModuleName();
        $controller = $this->request->getControllerName();
        $action     = $this->request->getActionName();
        $route      = $this->request->getRouteName();

        echo $moduleName."<br/>";
        echo $controller."<br/>";
        echo $action."<br/>";
        echo $route."<br/>";

        $this->_view->loadLayout();
        $this->_view->renderLayout();
    }
}

cześć @ Manashvi, czy możemy uzyskać nazwę kontrolera i akcji z referralUrl?
jafar pinjar

14

aby uzyskać phtmlplik lub controllerużyj poniżej

echo $controllerName = $this->getRequest()->getControllerName();
echo $actionName = $this->getRequest()->getActionName();
echo $routeName = $this->getRequest()->getRouteName();
echo $moduleName = $this->getRequest()->getModuleName(); 

Jak uzyskać działanie kontrolera strony głównej, aby ustawić obserwatora?
supriya mishra

jeśli przetestujesz ten kod, wyświetli się strona główna, controller:index,action:index,route:cms,module:cmsmam nadzieję, że to pomoże.
Qaisar Satti

@QaisarSatti, czy możemy uzyskać nazwę kontrolera i akcji z adresu URL polecenia? $ this-> redirect-> getRefererUrl ();
jafar pinjar

5

Użyj poniższych fragmentów kodu do phtml, kontrolera i zdarzeń w Magento 2

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$requestInterface = $objectManager->get('Magento\Framework\App\RequestInterface');

$routeName      = $requestInterface->getRouteName();
$moduleName     = $requestInterface->getModuleName(); 
$controllerName = $requestInterface->getControllerName(); 
$actionName     = $requestInterface->getActionName();

3
Nie należy ObjectManagerbezpośrednio tworzyć instancji . Powinieneś wstrzyknąć potrzebne klasy / obiekty przez DI.
7ochem

4

Możesz także:

$this->_requestInterface->getFullActionName()

Aby uzyskać pełną nazwę działania


1

Możesz uzyskać te informacje z obiektu żądania.

Przykład

W twojej controllerklasie:

$routeName        = $this->getRequest()->getRouteName();
$moduleName       = $this->getRequest()->getModuleName();
$controllerName   = $this->getRequest()->getControllerName();
$actionName       = $this->getRequest()->getActionName();

Mam nadzieję, że to pomoże.

Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.