Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 6 down vote accepted

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:

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();
share|improve this answer
    
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 – test Jan 6 at 7:45
    
Thanks Marius. Its working. – Nilesh Tighare Jan 7 at 11:47
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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.