zf2正则表达式路由

时间:2015-01-28 12:49:32

标签: regex routing zend-framework2

我正在努力与正则表达式路由.... 以下网址应该有效:

  • /店铺/ API /列表
  • /店铺/ API /购物车
  • /店铺/ API /登录
  • /店铺/ API /信息/ 0123456789

详细信息网址,最后一个,并不真正与以下路线合作:

            'child_routes' => array(
                'angularlist' => array(
                    'type' => 'Regex',
                    'options' => array(
                        'regex'    => '/api/(?<page>login|cart|list|details(?<id>\/[0-9]+))',
                        'spec' => '/api/%page%[/:%id%]',
                        'defaults' => array(
                            '__NAMESPACE__' => 'Shop\Controller',
                            'controller'    => 'Api',
                            //'action'        => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'        => 'list',
                        //  'param'         => '[0-9]',
                        ),
                        /* todo ipv elke child toe te voegen 'route'    => '/api/[:action]', 'constraints' => array(
                            'action' => 'list|details|login|dashboard'
                        )*/
                    ),
                ),
             )

1 个答案:

答案 0 :(得分:2)

而不是与不太可读的&amp;脏的正​​则表达式黑客攻击,您可以轻松实现如下所示的Segment路由堆栈:

'shop' => array(
    'type'    => 'Segment',
    'options' => array(
        'route'    => '/shop/api',
        'defaults' => array(
            '__NAMESPACE__' => 'Shop\Controller',
            'controller'    => 'Api',
            'action'        => 'index'
        ),
    ),
    'may_terminate' => true,
    'child_routes'  => array(
        'angularlist' => array(
            'type'    => 'Segment',
            'options' => array(
                'route'       => '/:action[/:id]', // id is optional
                'constraints' => array(
                    'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                    'id'         => '[0-9]+',
                ),
                'defaults' => array(
                    'action' => 'index',
                ),
            ),
        ),
    )