Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : MvcEvent – get __NAMESPACE__ / Module Name from Layout

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on July 27, 2012

I just created a module named ZfCommons that can initialize common functionality over module. For this post, i want to introduce a set ‘layout variable’ functionality with example : __NAMESPACE__, so we can echo-ing the module name / __NAMESPACE__ from layout.

First, just create a module named ZfCommons and register into the application.config.php :

return array(
    'modules' => array(
    'module_listener_options' => array(
        'config_glob_paths'    => array(
        'module_paths' => array(

And then, write the functions in Module class :

namespace ZfCommons;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface,

class Module implements

    public function onBootstrap($e)
        $eventManager        = $e->getApplication()->getEventManager();
        $eventManager->attach('dispatch', array($this, 'loadConfiguration' ));

    public function loadConfiguration(MvcEvent $e)
        $controller = $e->getTarget();
        $controllerClass = get_class($controller);
        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));

        //set 'variable' into layout...
        $controller->layout()->modulenamespace = $moduleNamespace;

    public function getAutoloaderConfig(){ /* common code */ }
    public function getConfig(){/* common code */}

And in the layout, You can call :

Current Module Namespace is <?php echo $this->modulenamespace; ?>

How about call the variable for view, this is it :

Current Module Namespace is <?php echo $this->layout()->modulenamespace; ?>

You can setting variable using this too :

$e->getViewModel()->setVariable('modulenamespace', $moduleNamespace);

References :
1. http://zend-framework-community.634137.n4.nabble.com/ZF2-What-s-the-proper-way-to-register-global-MVC-plugins-td4265227.html
2. http://akrabat.com/zend-framework-2/access-view-variables-in-another-view-model/

12 Responses

Subscribe to comments with RSS.

  1. […] MvcEvent – get __NAMESPACE__ / Module Name from Layout […]

  2. Jose Manuel Orts said, on January 24, 2013 at 4:37 pm

    Thank you very much ,I was looking for this and I solved my code with your post.
    Good job!!

  3. Abdou said, on April 25, 2013 at 8:22 pm

    I need to Acess view in another view model, can you help me plz

  4. Pd said, on September 2, 2014 at 12:46 pm

    Can I use the ZF2 Header and Footer in my external standard PHP application?

  5. Vipul said, on October 27, 2014 at 4:15 pm

    how can I use it and store it in variable inside onbootstrap function like this

    $var = $eventManager->attach(‘dispatch’, array($this, ‘loadConfiguration’ ));
    if($var == ‘module1’){
    //redirect here

    if($var == ‘module2’){
    //redirect here

    Its not working and Im using it directly withput events it displays modulename = ‘zend’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: