ZendFramework:简单的Router_Regex问题

时间:2011-05-08 23:44:51

标签: regex zend-framework

我从未擅长正则表达。我正在尝试使用它们来构建一个简单的站点。我构建一个像/some-course/some-vtm-1一样好的URL,但是当它试图查找定义的控制器时,它就失败了。这是我定义的路线:

chapter' => array(
                'type'  => 'Zend_Controller_Router_Route_Regex',
                'route' => '/:course/:vtm\-(\d+)',
                'defaults' => array(
                    'module'     => 'learn',
                    'controller' => 'chapter',
                    'action'     => 'index'
                ),
                'map' => array(
                    1 => 'course',
                    2 => 'vtm',
                    3 => 'id'
                ),
                'reverse' => '%s/%s-%d/'
            ),

当我点击/some-course/some-vtm-1之类的链接时,如何更正此正则表达式以便找到正确的模块/控制器/操作

1 个答案:

答案 0 :(得分:3)

你的问题是你试图混合Zend_Controller_Router_Route(在:开头的路线中的命名变量)和Zend_Controller_Router_Route_Regex(在路线中括起来的正则表达式模式)的语法。你想放弃前者并只使用regexp语法,留下这样的东西:

array(
    'type'  => 'Zend_Controller_Router_Route_Regex',
    'route' => '([\w]+)/(vtm)-([\d]+)',
     'defaults' => array(
        'module'     => 'learn',
        'controller' => 'chapter',
        'action'     => 'index'
     ),
     'map' => array(
         1 => 'course',
         2 => 'vtm',
         3 => 'id'
     ),
     'reverse' => '%s/%s-%d'
 ),