简体   繁体   中英

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',
                    ),
            ),
);

There is few things that need to be make sure is:-

You have to add your module in

  • application.config.php (which you are saying you done it.)
  • Security\\Controller\\Security has to be same in default too (which you already has)
  • One more thing is Your folder structure.... -

Just to doulbe check you have a /MODULE/src/ MODULE /Controller/CONTROLLER_FILE_NAME.php

I hope that helps..

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.

I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php" file with " <?php return array();?> " inside of it.

A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:

 $config         = $this->serviceLocator->get('config');
 var_dump($config);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM