ZF2 捕捉所有不匹配任何路线的东西

ZF2 catch all that don't match any route

在我的 Api 中,我在 module.config.php 中指定了多个子路由。使用无效路由访问 Api 时,我得到应用程序模块的 404,但我想 return json 中的 404。 我的想法是捕获所有无效路由并转发到控制器操作以 return 有效 json 404 响应。

我的路由配置如下所示:

'router' => array(
        'routes' => array(
            'api-host' => array(
                'type'    => 'Hostname',
                'options' => array(
                    'route'    => 'api.efeedback.de',
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'index' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/',
                            'defaults' => array(
                                'controller' => 'Api\Controller\Index',
                                'action' => 'index',
                            ),
                        ),
                    ),
                    'imports' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/imports/:product_id',
                            'constraints' => array(
                                'product_id' => '[0-9]+',
                            ),
                            'defaults' => array(
                                'controller' => 'Api\Controller\Import',
                            ),
                        ),
                    ),
                    'reports' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/reports',
                            'defaults' => array(
                                'controller' => 'Api\Controller\Report',
                                'auth' => true,
                            ),
                        ),
                        'may_terminate' => false,
                        'child_routes' => array(
                            'ratings' => array(
                                'type' => 'Segment',
                                'options' => array(
                                    'route' => '/ratings',
                                    'defaults' => array(
                                        'controller' => 'Api\Controller\Report',
                                        'auth' => true,
                                    ),
                                ),
                                'may_terminate' => false,
                                'child_routes' => array(
                                    'products' => array(
                                        'type' => 'Segment',
                                        'options' => array(
                                            'route' => '/products/:product_id',
                                            'constraints' => array(
                                                'product_id' => '[0-9]+',
                                            ),
                                            'defaults' => array(
                                                'controller' => 'Api\Controller\Report',
                                                'action' => 'products'
                                            ),
                                        ),
                                        'may_terminate' => false,
                                        'child_routes' => array(
                                            'advisors' => array(
                                                'type' => 'Segment',
                                                'options' => array(
                                                    'route' => '/advisors[/:advisor_id]',
                                                    'constraints' => array(
                                                        'advisor_id' => '[0-9]+',
                                                    ),
                                                    'defaults' => array(
                                                        'controller' => 'Api\Controller\Report',
                                                        'action' => 'advisors'
                                                    ),
                                                ),
                                            ),
                                            'providers' => array(
                                                'type' => 'Segment',
                                                'options' => array(
                                                    'route' => '/providers[/:provider_id]',
                                                    'constraints' => array(
                                                        'provider_id' => '[0-9]+',
                                                    ),
                                                    'defaults' => array(
                                                        'controller' => 'Api\Controller\Report',
                                                        'action' => 'providers'
                                                    ),
                                                ),
                                            ),
                                        ),
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),

通配符类型以某种方式起作用,但不适用于所有路由。但通配符已弃用。

我怎样才能做到这一点?

我决定使用 Regex 路由类型并将其放在我的子路由的最顶部:

'catch-all-no-match' => array(
    'type' => 'Regex',
    'options' => array(
        'regex' => '(?<content>.+)',
        'defaults' => array(
            'controller' => 'Api\Controller\Index',
            'action' => 'catch-all-no-match',
        ),
        'spec' => '%content%',
    ),
),

不要把它放在其他地方,因为它会在检查其他路由之前匹配。