Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 417
Greg.Forbes Avatar asked Nov 24 '12 15:11

Greg.Forbes


3 Answers

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',
    ),
),
like image 118
Will H Avatar answered Oct 18 '22 20:10

Will H


Just find the answer... posting here, it may help some one... I assume that you have follow the example of album module provided by zf2 docs, and now adding new modules, like in my case 'Admin' and i add new controllers like admin,news etc,

Here is the routers settings...

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

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

            ),
        ),
    ),

And here add your controller..

'controllers' => array(
        'invokables' => array(
            'Admin\Controller\News' => 'Admin\Controller\NewsController',
            'Admin\Controller\Admin' => 'Admin\Controller\AdminController',

        ),
    ),

Now you can call your controllers with in your module , like here in my case

localhost/{foldername}/admin/news/index and localhost/{foldername}/admin/admin/index

Thanks

like image 28
Muhammad Sohail Avatar answered Oct 18 '22 18:10

Muhammad Sohail


I was in the same case, I found a solution as the previous post, but I think it's not the better way.

In order to add le "clientController" inside my routeur:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),
        'client' => array(
            'type' => 'Segment',
            'options' => array(
                'route' => '/client[/:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id' => '[0-9]+',
                ),
                'defaults' => array(
                    'controller' => 'Fcm\Controller\Client',
                    'action' => 'index',
                ),
            ),
        ),
    ),
),
like image 1
Eddz le Bijoutier Avatar answered Oct 18 '22 18:10

Eddz le Bijoutier