views:

79

answers:

1

Hello you all:

I have two routes that match a url with the same apparent pattern, the difference lies in the $actionRoute, this should only be matched if the variable :action on it equals 'myaction'.

If I go to /en/mypage/whatever/myaction it goes as expected through $actionRoute.
If I go to /en/mypage/whatever/blahblah it gets rejected by $actionRoute and matched by $genRoute.
If I go to /en/mypage/whatever it should be matched by $genRoute but it gets matched by $actionRoute instead throwing and exception because the action noactionAction() does not exist.

Don't know what I'm doing wrong, I'd appreciate your help.

        $genRoute = new Zend_Controller_Router_Route(
   ':lang/mypage/:var1/:var2', 
   array(
    'lang'   => '',
    'module'  => 'mymodule',
    'controller' => 'index',
    'action'  => 'index',
    'var1'   => 'noone',
    'var2'   => 'no'
   ),
   array(
    'var1'   => '[a-z\-]+?',
    'lang'   => '(es|en|fr|de){1}'
   )
  );
  $actionRoute = new Zend_Controller_Router_Route(
   ':lang/mypage/:var1/:action', 
   array(
    'lang'   => '',
    'module'  => 'mymodule',
    'controller' => 'index',
    'action'  => 'noaction',
    'var1'   => 'noone',
   ),
   array(
    'action'  => '(myaction)+?',
    'var'   => '[a-z\-]+?',
    'lang'   => '(es|en|fr|de){1}',
   )
  );
  $router->addRoute('genroute',$genRoute);
  $router->addRoute('actionroute',$actionRoute);
A: 

By providing a default value ('noaction') for action in your $actionRoute, you are making that variable optional. If you remove this it should all work fine. Also the 'var' key in the regexp patterns in your second route should probably be 'var1' as it is in your first.

So, you probably want your second route to be:

  $actionRoute = new Zend_Controller_Router_Route(
   ':lang/mypage/:var1/:action', 
   array(
    'lang'   => '',
    'module'  => 'mymodule',
    'controller' => 'index',
    'var1'   => 'noone',
   ),
   array(
    'action'  => '(myaction)+?',
    'var1'   => '[a-z\-]+?',
    'lang'   => '(es|en|fr|de){1}',
   )
  );
Tim Fountain
Hi Tim, it doesn't work, it throws an exception with the message "action is not specified".Thanks anyways.
elbicho
Strange, I've tested your example code and it does work for me with that fix in place. What version of ZF are you using? One other hacky solution would be to replace :action with 'myaction' in your $actionRoute, remove the regexp for action and hard code the action to 'myaction' in the first array.
Tim Fountain
I'm using 1.8.4PL1
elbicho