0

I have never been any good at regular expressions. I am trying to use them in building a simple site. I construct a URL just fine like /some-course/some-vtm-1, but when it tries to lookup the defined controller, it fails. Here is the route I have defined:

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/'
            ),

How should I correct this Regex so it finds the correct module/controller/action when I a link like /some-course/some-vtm-1 is clicked

1
  • Why do you have backslash before minus/dash? I think you do not need to escape - character. Commented May 9, 2011 at 5:27

1 Answer 1

3

Your problem is that you're trying to mix the syntax of Zend_Controller_Router_Route (named variables in the route starting with :) and Zend_Controller_Router_Route_Regex (bracketed regular expression patterns in the route). You want to drop the former and just use the regexp syntax, leaving you with something like this:

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'
 ),
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your solution just now and it was still not matching on this route. I noticed you had (vtm) in there as a literal string. I tried your way, and 'route' => '([\w]+)/([\w]+)-([\d]+)' which neither worked. I have hyphens in the first two path(s) before the trailing hyphen & integer, so I need to account for that as well.
Here is my working solution, I hope it's the "right" way to do it. 'route' => '([\w-]+)/([\w-]+)-([\d]+)',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.