404 error, Zend Framework 2 The requested URL coul

2019-08-16 17:14发布

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 !

6条回答
smile是对你的礼貌
2楼-- · 2019-08-16 17:31

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

查看更多
forever°为你锁心
3楼-- · 2019-08-16 17:32

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.

查看更多
Root(大扎)
4楼-- · 2019-08-16 17:34

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

查看更多
小情绪 Triste *
5楼-- · 2019-08-16 17:39

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 !

查看更多
放我归山
6楼-- · 2019-08-16 17:46

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.

查看更多
戒情不戒烟
7楼-- · 2019-08-16 17:50

You have a typo, try this:

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

Routes rather than router twice..

查看更多
登录 后发表回答