2

In magento 1 I can get module name using below code:

$this->_getModuleName()

How can I get module name in helper magento 2?

  • 3
    In what context do you execute that code? Controller? Block? Helper?... Please improve your question. – Vinai May 7 '16 at 10:32
  • @Vinai i am trying to get module name in observer and helper file – Prashant Valanda May 7 '16 at 10:39
3

You can still do that in M2.

$this->_getModuleName() would still work, provided your helper class extends \Magento\Framework\App\Helper\AbstractHelper (which most of the time it should, anyway).

PS: As for fetching the module name in observers, that's a different story. One which needs more details. Or another question even. Do you want to get the module of the current observer? You could implement something similar to what \Magento\Framework\App\Helper\AbstractHelper::_getModuleName() does.

2

Try following code:

echo $moduleName = $this->getRequest()->getModuleName(); 
  • Thanks for your reply but not working – Prashant Valanda May 7 '16 at 10:28
  • From request it's possible inside Controller or Observer class I think, but not Helper – Bartosz Kubicki Jun 6 '16 at 8:24
  • If you add that model in helper you will be able to call it in helper too – DEEP JOSHI Jun 6 '16 at 8:48
1
public function __construct (
     ....
     \Magento\Framework\App\Request\Http $request,
     ....
) {   
     ....
     $this->_request = $request;
     ....
}

To get module name :

public function getModuleName()
{
     return $this->_request->getModuleName();
}
0

If you want to get module name at block class then you can get and that block class have Magento\Framework\View\Context; class object then using getModuleName(),you will get module name

  • I am trying to get module name in observer and helper of my custom module – Prashant Valanda May 7 '16 at 10:39

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.