【问题标题】:ZF2 cannot match dynamic wildcard route for breadcrumbZF2 无法匹配面包屑的动态通配符路由
【发布时间】:2016-08-27 17:39:05
【问题描述】:

我的系统具有全局动态路由,允许使用相同的代码样式开发模块。 我想为 /checkout/list/cart-type/2 这样的 url 生成面包屑,但导航配置与我的 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
                    ],
                ],
            ],
        ],
    ],
],  

【问题讨论】:

  • 路由/checkout/list/cart-type/2是否被路由器正确处理?

标签: php routes navigation zend-framework2


【解决方案1】:

您已将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
];

【讨论】:

  • 拨打$url('default/wildcard', [...]);会发生什么?
  • 我得到了以下输出结果/checkout/list/cart-type/2
  • 所以路线没问题,这是您的导航配置至极。您的问题来自您对Zend\Navigation 的使用,我从未使用过,所以我无法帮助您。我猜你已经知道文档docs.zendframework.com/zend-navigation/intro
【解决方案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' => [],
                ],
            ],
        ],
    ]
]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多