为什么我的 ZF2 Segment 子路由被忽略了?

Why is my ZF2 Segment child route being ignored?

我的项目中有以下路由配置:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'MyApp\Controller',
                    'controller' => 'Default',
                    'action' => 'default',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'api' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => 'api/:action[/:id]',
                        'defaults' => array(
                            '__NAMESPACE__' => 'MyApp\Controller',
                            'controller' => 'Api',
                            'action' => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                ),
                'default' => array(
                    'type' => 'Zend\Mvc\Router\Http\Segment',
                    'options' => array(
                        'route' => '[:id]',
                        'defaults' => array(
                            '__NAMESPACE__' => 'MyApp\Controller',
                            'controller' => 'Default',
                            'action' => 'default',
                        ),
                    ),
                    'may_terminate' => true,
                ),
            ),
        ),
    ),
),

当我调用 http://localhost/api/foo/bar 时,我得到了 DefaultController 的响应。我已经剥离了我的应用程序,直到这是唯一的路径(删除了 home/default,并使其成为应用程序的唯一路径)但它被忽略了。

期望的结果是调用 /api... 转到 Api 控制器,但所有其他调用转到默认控制器。

据我所知(查看 apache2 日志)没有错误被抛出

关于可能出现的问题有什么建议吗?

我认为这是因为您的默认路由 ID 没有任何限制,所以它匹配所有内容。尝试在默认路由后添加它。

'constraints' => ['id' => '[0-9]'],