Zf2子路由匹配不起作用

时间:2014-01-23 16:12:46

标签: php zend-framework2

目标是有两条路线。

  1. domain.de/wordWithMinFourLetters
  2. domain.de/wordWithMaxThreLetters

  3. domain.de/stackoverflow

  4. domain.de/ch
  5. 路由1默认操作是业务 路线2默认操作是国家/地区

    问题:

    路线2总是被执行,我不知道为什么。 我的路线配置有什么错误?

    'router' => array(
        'routes' => array(
            'application' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'index',
                        'action' => 'index'
                    )
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'business' => array(
                        'type' => 'segment',
                        'options' => array(
                            'route' => ':business',
                            'constraints' => array(
                                'business' => '[a-z]{4,10}'
                            ),
                            'defaults' => array(
                                'action' => 'business'
                            )
                        )
                    ),
                    'countries' => array(
                        'type' => 'segment',
                        'options' => array(
                            'route' => ':countries',
                            'constraints' => array(
                                'countries' => '[a-z]{2,3}'
                            ),
                            'defaults' => array(
                                'action' => 'countries'
                            )
                        )
                    )
                )
            ),
            'clear' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/clear',
                    'defaults' => array(
                        'controller' => 'index',
                        'action' => 'clear'
                    )
                )
            ),
            'sitemap' => array(
                'type' => 'literal',
                'options' => array(
                    'route' => '/sitemap',
                    'defaults' => array(
                        'controller' => 'index',
                        'action' => 'sitemap'
                    )
                )
            )
        )
    )
    

1 个答案:

答案 0 :(得分:1)

替换一些约束

  'application' => array(
        'type' => 'Zend\Mvc\Router\Http\Literal',
        'options' => array(
            'route' => '/',
            'defaults' => array(
                'controller' => 'index',
                'action' => 'index'
            )
        ),
        'may_terminate' => true,
        'child_routes' => array(
            'business' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '/:business',   // <--- update
                    'constraints' => array(
                        'business' => '[a-z]{4,10}' // <-----
                    ),
                    'defaults' => array(
                        'controller' => 'index', // <--- update
                        'action' => 'business'
                    )
                )
            ),
            'countries' => array(
                'type' => 'Zend\Mvc\Router\Http\Segment',
                'options' => array(
                    'route' => '/:countries', // <--- update
                    'constraints' => array(
                        'countries' => '[a-z]{2,3}'  // <-----
                    ),
                    'defaults' => array(
                        'controller' => 'index', // <--- update
                        'action' => 'countries'
                    )
                )
            )
        )
    ),