【问题标题】:Zend Framework 2 (ZF2) 404 Page not found error when calling actionZend Framework 2 (ZF2) 404 Page not found 调用动作时出错
【发布时间】:2013-03-26 18:50:09
【问题描述】:

我是从ZF2 Skeleton Application开始的。我有一个名为Application 的模块。在该模块中,我添加了自己的ShopController。它注册在module.config.php,看起来像这样。

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
            ),
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/application',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'Index',
                        '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(
                            ),
                        ),
                    ),
                ),
            ),
            'shop' => array(
                    'type'    => 'segment',
                    'options' => array(
                            'route'    => '/shop[/][:action][/:id]',
                            'constraints' => array(
                                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'id'     => '[0-9]+',
                            ),
                            'defaults' => array(
                                    '__NAMESPACE__' => 'Application\Controller',
                                    'controller'    => 'Shop',
                                    'action'        => 'index',
                            ),
                            '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(
                                                    ),
                                            ),
                                    ),
                            ),
                    ),
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Shop' => 'Application\Controller\ShopController',
        ),
    ),
    [... service_manager,translator,view_manager as in skeleton app ...]
);

我可以通过浏览到 my.example.com/shop/ 来访问商店控制器的索引操作,但是当我尝试执行其他操作时,例如my.example.com/shop/add 它不起作用。我得到一个 404。

我错过了什么?

【问题讨论】:

  • 我也是 zend 的新手,但在您的 ShopController 类中,您是否有方法 addAction 返回视图或空数组()
  • 感谢您指出,是的,它就在那里。 :)
  • 并在视图目录下添加.phtml ?

标签: php zend-framework2


【解决方案1】:

我尝试添加 may_terminate = true 并且它正在工作,同时删除 child_routes 也可以工作。

 'shop' => array(
            'type'    => 'segment',
            'options' => array(
                'route'    => '/shop[/][:action][/:id]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'     => '[0-9]+',
                ),
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    'controller'    => 'Shop',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true, // I added this
            '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(
                        ),
                    ),
                ),
            ),
        ),

【讨论】:

  • 谢谢!我似乎在错误的地方定义了子路由。
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 2021-12-21
  • 2011-12-13
相关资源
最近更新 更多