knot
knot

Reputation: 85

Routes in zend framework 2

I am trying hardly to put the routes for some controllers in zend framework 2 and even after I read a lot I can't figure why it still tells 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 of them having 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.

Upvotes: 3

Views: 6072

Answers (2)

Crisp
Crisp

Reputation: 11447

If I'm understanding the question and your structure correctly, you need to set up the namespaces in your 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',
            ),
        ),
    );
}

Upvotes: 2

DrBeza
DrBeza

Reputation: 2251

At the top of your config you have Controller invokables configuration:

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

The first value in the above is an identifier. This is what you are meant to use within your route definitions. For example your contacts route - try changing the following:

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

Upvotes: 2

Related Questions