Search code examples
phpzend-framework2zend-framework-moduleszend-framework-routing

Unable to route with module in ZF2


I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.

I've got three modules loaded: Application, Frontend and Security.

Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).

The module is also loaded in application.config.php.

Any ideas on what I'm missing?

Module Config:

return array(
            'controllers' => array(
                    'invokables' => array(
                            'Security\Controller\Security' => 'Security\Controller\SecurityController',
                    ),
            ),

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

            'view_manager' => array(
                    'template_path_stack' => array(
                            'security' => __DIR__ . '/../view',
                    ),
            ),
);

Solution

  • I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.

    I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.

    I'm pretty sure this was not the best way to debug an application's configuration.