2

I want to get current module name in its controller action method

Any help is greatly appreciated

 public function initAction()
{
  parent::initAction();

 die(var_dump(module name here));   
}

Thanks!

2 Answers 2

4

I think easiest way to getting current module name is using magic __NAMESPACE__ constant.

In your controller, try this:

$modulename = explode('\\', __NAMESPACE__, -1);
var_dump($modulename); // [0] => 'Application'

You can also get a list of loaded modules via ModuleManager like this:

$manager = $this->getServiceLocator()->get('ModuleManager');
$manager->getLoadedModules();
Sign up to request clarification or add additional context in comments.

2 Comments

I really love the idea of module manager clean and concise but how i can get the name of current module from $manager->getLoadedModules();
@PradeepSrivastava The answer is you can't. $manager->getLoadedModules(); returns an array which contains the names of the loaded modules. You can for example check weither a module is loaded or not (if(in_array('moduleName',$manager->getLoadedModules())))
2

You can get it from the current namespace.

Within your controller action you can do this :

public function initAction()
{
  parent::initAction();
  $controllerClass = get_class($this);
  $moduleName = substr($controllerClass, 0, strpos($controllerClass, '\\'));
  die($moduleName);  
}

Example :

  • namespace : Administration\Controller;
  • Output : Administration

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.