shaxaaa
shaxaaa

Reputation: 151

ZF2 Module (Application) could not be initialized

I have application.config.php which is 100% right:

return array(
    'modules' => array(
        'Application',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{local,global}.php',
        ),
        'config_cache_enabled' => false,
        'cache_dir' => 'data/cache',
        'module_paths' => array(
            realpath(__DIR__ . '/../module'),
        ),
    )
);

Then I get this error:

Module (Application) could not be initialized.

I have followed the error and it seem that ModuleAutoloader isn't loading my files.

The $this->paths array is right to the module folder. My Module file is in module/Application/Module.php

It is a namespaced application and the class is Module. I just can't get what the problem might be.

<?php
namespace Application;

class Module
{
    /**
     * Module directory path
     *
     * @var string
     */
    protected $directory = null;

    /**
     * Module namespace
     *
     * @var string
     */
    protected $namespace = null;

    /**
     * Module configuration
     *
     * @var array
     */
    protected $config;

    /**
     * Get autoloader config
     *
     * @return array
     */
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                $this->getDir() . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    $this->getNamespace() => $this->getDir() . '/src/' . $this->getNamespace(),
                ),
            ),
        );
    }

    /**
     * Get module configuration
     *
     * @return array
     */
    public function getConfig()
    {
        if (empty($this->config)) {
            $config       = include $this->getDir() . '/config/module.config.php';
            $this->config = $config;
        }

        return $this->config;
    }

    /**
     * Get module dir
     *
     * @return string
     */
    protected function getDir()
    {
        return $this->directory;
    }

    /**
     * get module namespace
     *
     * @return string
     */
    protected function getNamespace()
    {
        return $this->namespace;
    }
}

Upvotes: 1

Views: 4214

Answers (2)

Carlos Robles
Carlos Robles

Reputation: 10947

Try using magic constants instead of the functions for the namespaces and dirs, like this

public function getAutoloaderConfig() {
    return array (
            'Zend\Loader\StandardAutoloader' => array (
                    'namespaces' => array (
                            __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__ 
                    ) 
            ) 
    );
}

And in your application.config.php, you can simplify it this way:

'module_paths' => array(
        './module', 
    ),

if this does not work, you can try to hard code the full module path to see if there is something wrong with the autoloader:

'module_paths' => array(
    './module',            
    'Application' => './module/Application/src/Application' 
),

Upvotes: 1

Prabhu Patil
Prabhu Patil

Reputation: 16

Change 'module_paths' to:

'module_paths' => array(
    '../../module',
    '../../vendor',
)

Upvotes: 0

Related Questions