I’m a little at loss with CakePHP(1.3) routes when it comes to default values and persistent parameters.
My current project reads the current language from the first parameter of the url (example.com/en/pagename/) and in case the language is not available, it tries to guess the language from session or cookies and if that fails, falls back to default configuration.
After this, all the urls generated with Router::url() should contain the language parameter(that is, the default language if not overridden in arguments). The problem is, I don’t know a clean way to achieve this. Is there one or do I need to add my own hacks? What would be the best place to apply these hacks without affecting all other routes?
I could probably solve this particular problem with redirects that fill in missing parameters, but I’d like to learn more about routing in CakePHP and I feel that these default values could be useful in other applications too.
I solved the problem by making the language parameter optional and checking for it in
appController‘sbeforeRender(). If a language code was not present, the browser will be redirected to the current url with language code added.