Routes in zend framework 2

I try not to confuse routes for some controllers in zend framework 2, and even after reading a lot, I can't figure out why it is still telling me. The requested controller could not be mapped to an existing controller class. I have a module named CRM and in the src folder I have Contacts and Companies, each with a controller / form / model. This is my module.config file:

     array(
         'controllers' => array(
         'invokables' => array(
              'CRM\Controller\Contacts' => 'CRM\Controller\ContactsController',
          'CRM\Controller\Companies' => 'CRM\Controller\CompaniesController',
    ),
),


'router' => array(
    'routes' => array(
        'contacts' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'    => '/contacts[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Contacts\Controller\Contacts',
                    'action'     => 'index',
                ),
            ),
        ),

        'companies' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/companies[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Companies\Controller\Companies',
                    'action'     => 'index',
                ),
            ),
        ),
    ),
),



'view_manager' => array(
    'template_path_stack' => array(
        'contacts' => __DIR__ . '/../view/crm',
        'companies' => __DIR__ . '/../view/crm',
    ),
),

      

);

Any help would be really appreciated.

+3


source to share


2 answers


If I understand the question and your structure correctly, you need to configure the namespaces in the autoloader config ...



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

      

+2


source


At the top of the config, you have the controller invokables config:

'CRM\Controller\Contacts' => 'CRM\Controller\ContactsController',

      



The first value in this case is the identifier. This is what you should use in your route definitions. For example your route contacts

- try changing the following:

'defaults' => array(
     'controller' => 'CRM\Controller\Contacts',
     'action'     => 'index',
),

      

+2


source







All Articles