Zend Framework 2:一个动作的多个路由

时间:2013-03-14 12:17:48

标签: php routing get zend-framework2

在Zend Framework 2中执行以下操作是否有更好的解决方案:

我需要这两个网址转到一个操作 - help / section / 5和help / section.php?id = 5

我认为这种方式太复杂了:

        'helpSection' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route'    => '/help/section/:id',
                'defaults' => array(
                    'controller' => 'Application\Controller\Help',
                    'action'     => 'section',
                ),
            ),
        ),
        'helpSection2' => array(
            'type' => 'Zend\Mvc\Router\Http\Segment',
            'options' => array(
                'route'    => '/help/section.php',
                'defaults' => array(
                    'controller' => 'Application\Controller\Help',
                    'action'     => 'section',
                ),
            ),
            'child_routes'  => array(
                'query' => array(
                    'type' => 'Zend\Mvc\Router\Http\Query',
                    'options' => array(
                        'defaults' => array(
                            'id' => ':id'
                        )
                    )
                ),
            ),
        ),

1 个答案:

答案 0 :(得分:0)

未经测试,但这可能确实有效...它应该符合以下条件:

  • /帮助/部分
  • /帮助/部分?的queryString
  • /help/section.php
  • /help/section.php?queryString
  • /帮助/部分/ 1
  • /帮助/部分/ 1?的queryString

这将是配置:

'type' => 'segment',
'options' => array(
    'route' => '/help/section[(.php|/:id)]',
    'defaults' => array(
        'controller' => '...',
        'action' => '...'
    ),
    'constraints' => array(
        'id' => '[0-9]+'
    )
),
'may_terminate' => true,
'child_routes' => array(
    'query' => array(
        'type' => 'query'
    )
)