404 error, Zend Framework 2 The requested URL coul

2019-08-16 17:23发布

问题:

I'm getting:

A 404 error occurred

Page not found. The requested URL could not be matched by routing.

My module.config.php file is:

'router' => array(
    'router' => array(
        'Test' => array(
            'type' => 'Segment',
            'options' => array(
                //http://localhost/Test/Test
                'route' => '/Test[/[:action]]',
                'constraints' => array(
                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
             ),
             'defaults' => array(
                 'controller' => 'Test\Controller\Test',
                 'action' => 'Test'
              ),
          ),
      ),
   ),
),

help please, i am new in Zend Framework 2 !

回答1:

You should use configuration like Application module in ZendSkeletonApplication:

'router' => array(
    'routes' => array(
        'test' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/book',
                'defaults' => array(
                    '__NAMESPACE__' => 'Test\Controller',
                    'controller'    => 'Test',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

You just add the following code:

'test' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '[/:action][/:id]',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id'     => '[0-9]*',
                        ),
                        'defaults' => array(
                            '__NAMESPACE__' => 'Test\Controller',
                            'controller'    => 'Test',
                            'action'        => 'index',
                        ),
                    ),
                ),

add this code to 'child-routes' key and after that you'll access to url: localhost/:controller_name/:action_name/:id (example : http://zf.dev/test/index or http://zf.dev/test/add/1). And now it's work! This code can fix error 404 for tutorial in zf2 documentation.



回答2:

You have a typo, try this:

'router' => array(
    'routes' => array(

Routes rather than router twice..



回答3:

Achieve fix it, I was missing the letter "d", was thus: Zend \ Loader \ StandarAutoloader I added the "d": Zend \ Loader \ StandardAutoloader. Greetings Friends. TIP: Zend Studio 10 and his version de ZF2 run perfect for this moment !



回答4:

Please check the .htaccess file and index.php files. If these are exist in public folder means, you have to use the url as

 http://localhost/public/Test/Test.

Your codes are almost right. Andrew has guided you well. Let me know your response.



回答5:

1.You should check also application.config.php and add your module name into RETURN array.

return array(    
    'modules' => array(
        'Application',
        'your_module',
        .....    
 ),

2.If Doesn't.Check route array in module.config.php



回答6:

I will suggest also check the data folder that has cached config files, config files being cached in dev install also may cause this issue. delete files inside data/cache and try.

PS: if you are just starting try with blog module on zend site it is for beginners and more updated with new versions.

https://framework.zend.com/manual/2.4/en/in-depth-guide/first-module.html