ZF2不能匹配面包屑的动态通配符路由


ZF2 cannot match dynamic wildcard route for breadcrumb

我的系统有全局动态路由,允许使用相同的代码风格开发模块。我想为这样的url生成面包屑/checkout/list/cart-type/2,但导航配置不能匹配我的url。

另一方面,当我简单地路由到/checkout/list时,它可以正常工作。

请帮我正确配置我的配置。

我的路由器配置

'router' => [
    'routes' => [
        'default' => [
            'type' => 'Segment',
            'options' => [
                'route' => '/[:controller[/[:action]]]', // global route
                'constraints' => [
                    'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
                    'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
                ],
                'defaults' => [
                    'controller' => 'index',
                    'action' => 'index',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'wildcard' => [
                    'type' => 'Wildcard',
                    'priority' => 10,
                    'options' => [],
                ],
            ],
        ],
    ],
],

我的导航配置

'navigation' => [
    'default' => [
        'checkout' => [
            'module' => 'checkout',
            'label' => 'Home',
            'route' => 'default',
            'controller' => 'index',
            'action' => 'index',
            'pages' => [
                'checkout-list' => [
                    'label' => 'Invoices',
                    'route' => 'default/wildcard',
                    'controller' => 'checkout',
                    'action' => 'list',
                    'params' => [
                        'cart-type' => 2
                    ],
                ],
            ],
        ],
    ],
],  

您定义了controlleraction作为参数,因此尝试(未测试):

'navigation' => [
    'default' => [
        'checkout' => [
            'module' => 'checkout',
            'label' => 'Home',
            'route' => 'default',
            'controller' => 'index',
            'action' => 'index',
            'pages' => [
                'checkout-list' => [
                    'label' => 'Invoices',
                    'route' => 'default/wildcard',
                    'params' => [
                        'controller' => 'checkout',
                        'action' => 'list',
                        'cart-type' => 2
                    ],
                ],
            ],
        ],
    ],
],

或:

$url('default/wildcard', [
    'controller' => 'checkout',
    'action' => 'list',
    'cart-type' => 2
];

我找到解决办法了。

重新定义通配符路由的'id'子段路由出现问题

'default' => [
    'type' => 'Segment',
    'options' => [
        'route' => '/[:controller[/[:action]]]', // global route
        'constraints' => [
            'controller' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
            'action' => '[a-zA-Z]?[a-zA-Z0-9_-]*',
        ],
        'defaults' => [
            'controller' => 'index',
            'action' => 'index',
        ],
    ],
    'may_terminate' => true,
    'child_routes' => [
        'id' => [
            'type' => 'Segment',
            'priority' => 100,
            'options' => [
                //'route' => '[/:id]', // this was changed without brackets (!)
                'route' => '/:id',
                'constraints' => [
                    'id' => '[0-9]+',
                ],
                'defaults' => [
                    'id' => '0',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'wildcard' => [
                    'type' => 'Wildcard',
                    'options' => [],
                ],
            ],
        ],
    ]
]