2

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

$this->_getModuleName()

How can I get module name in helper magento 2?

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

4 Answers 4

5

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.

1
  • this should be accepted solution Jun 6, 2016 at 8:23
2

Try following code:

echo $moduleName = $this->getRequest()->getModuleName(); 
3
  • Thanks for your reply but not working May 7, 2016 at 10:28
  • From request it's possible inside Controller or Observer class I think, but not Helper Jun 6, 2016 at 8:24
  • If you add that model in helper you will be able to call it in helper too
    – DEEP JOSHI
    Jun 6, 2016 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

1
  • I am trying to get module name in observer and helper of my custom module May 7, 2016 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.