Search code examples
phpzend-frameworkzend-framework2

zend framework 2 nested module structure in one main module


I am trying to create a app using zend framework 2. Have used structure as follow. Getting error class not found. Trying to created nested sub module login inside a main module called album.

I have following structure:

Album
    - src
        - Album
            - Controller
                - AlbumController.php
            - Form
                - AlbumForm.php
            - Model
        - Login
            - Controller
                - LoginController.php
            - Form
                - LoginForm.php
            - Model

Getting following error:

Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "logincontrollerlogin(alias: Login\Controller\Login)" via invokable class "Login\Controller\LoginController"; class does not exist

Routing is as follow:

return array(
    'controllers' => array(
        'invokables' => array(
            'Album\Controller\Album' => 'Album\Controller\AlbumController',
            'Login\Controller\Login' => 'Login\Controller\LoginController',
        ),
    ),
    // The following section is new and should be added to your file
    'router' => array(
        'routes' => array(
            'login' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/login',
                    'defaults' => array(
                        'controller' => 'Login\Controller\Login',
                        'action'     => 'index',
                    ),
                ),
            ),
            'album' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action'     => 'index',
                    ),
                ),
            ),
        ),
    ),
    'view_manager' => array(
        'template_path_stack' => array(
            'album' => __DIR__ . '/../view',
            'login' => __DIR__ . '/../view',
        ),
    ), );

Can you please help me to resolve the above error:

Autoload in Module.php:

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

Solution

  • Your autoloader probably can't find your controller because it's under a different namespace. Try specifying the Login namespace and see if it helps.

        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                'Login' => __DIR__ . '/src/Login',
            ),
        ),