Zend Framework 2: Router "resolves to invalid controller class or alias:"

Greg.Forbes picture Greg.Forbes · Nov 24, 2012 · Viewed 17.6k times · Source

I am looking for some help with the ZF2 router configuration.

I would like to add multiple controllers to a single module in a development context, although will eventually have to figure out how to specify specific routes.

To do this I am currently trying to use the general purpose module.config specified in the MVC quick start documentation. However this does not seem to perform as the ZF2 documentation would suggest.

http://framework.zend.com/manual/2.0/en/modules/zend.mvc.quick-start.html

The documentation notes:

"ZendSkeletonApplication ships with a “default route” that will likely get you to this action. That route basically expects “/{module}/{controller}/{action}”, which allows you to specify this: “/zend-user/hello/world”. We’re going to create a route here mainly for illustration purposes, as creating explicit routes is a recommended practice."

However, the controller renders /landlord, /landlord/home/index, but not /landlord/sandbox/index. Home and sandbox are controllers. Sandbox has been named in line with the naming convention "SandboxController". My take is that perhaps the child_routes section of the code in the documentation needs some sort of modification that I have missed.

In the sandbox instance I get this error on the 404 error page.

Landlord\Controller\Sandbox(resolves to invalid controller class or alias: Landlord\Controller\Sandbox)

I tried adding 'Landlord\Controller\Sandbox' => 'Landlord\Controller\SandboxController', to the invokables, but this creates an error.

My controller is listed below:

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',

    ),
),
'router' => array(
    'routes' => array(
        'landlord' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/landlord',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Landlord\Controller',
                    'controller'    => 'home',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This route is a sane default when developing a module;
                // as you solidify the routes for your module, however,
                // you may want to remove it and replace it with more
                // specific routes.
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),
'view_manager' => array(
    'template_path_stack' => array(
        'landlord' => __DIR__ . '/../view',
    ),
),
);

If there is a simple way to directly configure urls, this would also be useful, or a really good tutorial this would also be appreciated.

Answer

Will H picture Will H · Nov 30, 2012

The problem here is that even though the router would try to route to your new controller that you added you still need to add the Invokable part for every single controller that you add. So your router appears to be fine at this point but if you add for instance a new AboutController then you will need to modify the top of your config like so...

return array(
'controllers' => array(
    'invokables' => array(
        'Landlord\Controller\Home' => 'Landlord\Controller\HomeController',
        'Landlord\Controller\About' => 'Landlord\Controller\AboutController',
    ),
),