1

Would you please let me know best way to get the Full Action name inside custom Model class in Magento 2?

13

First of all, your models should not depend on the request. You should find an other way of passing what you need from the request to the model.
But if you insist... you can do this.
Inject an instance of the request in the model constructor, like this:

protected $request;
public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->request = $request;
}

Then you can get the full action name like this:

$action = $this->request->getFullActionName();
  • I tried this code in my custom controller contruct method but not work.Any other pattern to create object of \Magento\Framework\App\Request\Http class – Niraj Patel Jan 6 '16 at 7:45
0
public function execute(\Magento\Framework\App\RequestInterface $request) {
if($request->getControllerName() == 'product' && $request->getModuleName() == 'catalog'){
    $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', 8);
}else if($request->getControllerName() == 'category' && $request->getModuleName() == 'catalog'){
    $request->setModuleName('cms')->setControllerName('page')->setActionName('view')->setParam('page_id', 7);
}else{
    $request->setModuleName('cms')->setControllerName('index')->setActionName('index'); 
}

it's working in controller

0

At Phtml you can use

$block->getRequest()->getFullActionName();

BlocK

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

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.