Multiple namespaces in one module in ZF2

I'm having trouble setting up multiple namespaces / classes in the same module. For example, I have a module called "Account" in which I would like to include all classes associated with the account (companies: "accounts", users: "users", external api: "api", etc.) . The structure of the module looks like this.

        /Account
        - Module.php
        - /config
        - /view
        - /src
          - /Account
            - /Controller (AccountController.php)
            - /Form       (AccountForm.php)
            - /Model      (Account.php + AccountTable.php)
          - /User
            - /Controller (UserController.php)
            - /Form       (UserForm.php)
            - /Model      (User.php + UserTable.php)
          - /Api
            - Api.php     (simple class)

Being new to ZF2, I decided to keep everything simple and stupid and not try to implement complex routing in the Account module. So, to call indexAction for UserController, the url must be / user (!)

Here's the module class:

namespace Account;

use Account\Model\AccountTable;
use Account\Model\UserTable;

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

    public function getServiceConfig()
    {
        return array(
                            'factories' => array(
                                                            'Account\Model\AccountTable'  =>  function($sm) {
                                                                                                                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                                                                                                                $table = new AccountTable($dbAdapter);
                                                                                                                                return $table;
                                                                                                                            },
                                                            'Account\Model\UserTable'           =>  function($sm) {
                                                                                                                                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                                                                                                                                $table = new UserTable($dbAdapter);
                                                                                                                                return $table;
                                                                                                                            },                                                              
                                                      ),
        );
    }    

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
}

And the module.config file

return array(
    'controllers' => array(
        'invokables' => array(
            'Account\Controller\Account'    => 'Account\Controller\AccountController',
            'Account\Controller\User'           => 'Account\Controller\UserController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'account' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'      => '/account[/:action[/:accountId]]',
                    'constraints' => array(
                                                    'action'         => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'accountId'      => '[0-9]+',
                                                ),
                    'defaults' => array(
                        'controller' => 'Account\Controller\Account',
                        'action'     => 'index',
                    ),
                ),
/*
                'may_terminate' => true,
                'child_routes' => array(
                      'user' => array(
                            'type' => 'literal',
                            'options' => array(
                                'route' => '/user[/:action[/:userId]]',
                                'constraints' => array(
                                                    'action'         => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'userId'         => '[0-9]+',
                                                ),
                                'defaults' => array(
                                        'controller' => 'Account\Controller\User',
                                        'action'     => 'index'
                                )
                        )
                    )
                ),
*/
            ),
            'user' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'      => '/user[/:action[/:userId]]',
                    'constraints' => array(
                                                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                    'userId'     => '[0-9]+',
                                                ),
                    'defaults' => array(
                        'controller' => 'Account\Controller\User',
                        'action'     => 'index',
                    ),
                ),
             )


        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'account' => __DIR__ . '/../view',
            'user'    => __DIR__ . '/../view',

        ),
    ),
);

, , , "Class 'Account\Controller\UserController" ". , - . , ?

+5
2

StandardAutoloader :

public function getAutoloaderConfig()
{
    return array(
        'Zend\Loader\ClassMapAutoloader' => array(
            __DIR__ . '/autoload_classmap.php',
        ),
        'Zend\Loader\StandardAutoloader' => array(
            'namespaces' => array(
                // This is for the Account namespace
                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                // And this is for the User namespace
                'User'        => __DIR__ . '/src/' . 'User',
            ),
        ),
    );
}

.config.php

return array(
    'controllers' => array(
        'invokables' => array(
            'Account\Controller\Account' => 'Account\Controller\AccountController',
            // The key can be what ever you want, but the value must be a valid
            // class name. Your UserController lives in the User namespace,
            // not in Account
            'Account\Controller\User'    => 'User\Controller\UserController',
        ),
    ),
    /* ... */
);
+11

Loader , . namespaces, , ( script) . :

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

__ NAMESPACE__ - , __DIR__ - Module.PHP

http://framework.zend.com/manual/2.0/en/modules/zend.loader.standard-autoloader.html

ClassMapAutoloader : , , zf2 , ( StandardLoader).

http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html

+1

All Articles