Friday, February 11, 2011

CakePHP Routing, using a language prefix with a default prefix

I'm trying to create a routing prefix that would be default.

http://localhost/heb/mycont would leave to the Hebrew page, while

http://localhost/mycont would lead to the English page.

Router::connect('/:language/mycont',array('controller'=>'contname','action'=>'index'),array('language'=>'[a-z]{0,3}'));

This code allows me to use 0-3 letters for language, but it still requires a language!

http://localhost/a/mycont would work

http://localhost/mycont doesn't work

Any ideas how to fix that? Is it even possible with the default routing?

  • Let me preface this by stating that I'm not a routing expert, but in this case, it makes sense that what you have wouldn't work because the route is expecting a language parameter; the route won't match if it's not there.

    To "fix" (quoted since it's not really broken), you might want to try setting your default locale and, in your AppController, overwrite if a :language value is present.

    Travis Leleu : This looks like a quick and solid solution to me! The only other reasonable option that I can think of would be to override the Router::connect function so that it will set the default :language value if none is present. But that might be overkill.
  • My solution was simply to setup the / to a specific language, while everything else is marked /:language/

    That way I did not make duplicated routes.

    From Yossi

0 comments:

Post a Comment