Module.php


class Module implements AutoloaderProvider                                                          
{                                                                                                   
    /**                                                                                             
     * @var Zend\Di\Di                                                                              
     */                                                                                             
    protected $locator;                                                                             
                                                                                                    
    /**                                                                                             
     * Initialize Module                                                                            
     *                                                                                              
     * @return void                                                                                 
     */                                                                                             
    public function init(Manager $manager)                                                          
    {                                                                                               
        $events = StaticEventManager::getInstance();                                                
        $events->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));                     
    }                                                                                               

    /**                                                                                             
     * Set Autoloader Configuration                                                                 
     *                                                                                              
     * @return array                                                                                
     */                                                                                             
    public function getAutoloaderConfig()                                                           
    {                                                                                               
        return array(                                                                               
            'Zend\Loader\ClassMapAutoloader' => array(                                              
                __DIR__ . '/autoload_classmap.php',                                                 
            ),                                                                                      
            'Zend\Loader\StandardAutoloader' => array(                                              
                'namespaces' => array(                                                              
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,                             
                ),                                                                                  
            ),                                                                                      
        );                                                                                          
    }                                                                                               
                                                                                                    
    /**                                                                                             
     * Get Configuration                                                                            
     *                                                                                              
     * @param string $env                                                                           
     * @return array                                                                                
     */                                                                                             
    public function getConfig($env = null)                                                          
    {                                                                                               
        return include __DIR__ . '/config/module.config.php';                                       
    }                                                                                               
                                                                                                    

    /**                                                                                             
     * On Bootstrap                                                                                 
     *                                                                                              
     * @param \Zend\EventManager\Event $e                                                           
     * @return void                                                                                 
     */                                                                                             
    public function onBootstrap(Event $e)                                                           
    {                                                                                               
        $app = $e->getParam('application');                                                         
        $this->locator = $app->getLocator();                                                        
        $app->events()->attach('dispatch', array($this, 'onDispatch'), -100);                       
    }                                                                                               
                                                                                                    
    /**                                                                                             
     * On Dispatch                                                                                  
     *                                                                                              
     * @param \Zend\EventManager\Event $e                                                           
     * @return void                                                                                 
     */                                                                                             
    public function onDispatch($e)                                                                  
    {                                                                                               
        $matches    = $e->getRouteMatch();                                                          
        $controller = $matches->getParam('controller');                                             
        if (strpos($controller, __NAMESPACE__) !== 0) {                                             
            // not a controller from this module                                                    
            return;                                                                                 
        }                                                                                           
        $templatePathStack = $this->locator->get('Zend\View\Resolver\TemplatePathStack');           
        $templatePathStack->addPath(__DIR__ . '/view');                                             
    }                                                                                               
}