What's the cleanest way to add a prefix to every URL in CakePHP, like a language parameter?
http://example.com/en/controller/action
http://example.com/ru/admin/controller/action
It needs to work with "real" prefixes like admin
, and ideally the bare URL /controller/action
could be redirected to /DEFAULT-LANGUAGE/controller/action
.
It's working in a retro-fitted application for me now, but it was kind of a hack, and I need to include the language parameter by hand in most links, which is not good.
So the question is twofold:
- What's the best way to structure Routes, so the language parameter is implicitly included by default without having to be specified for each newly defined Route?
Router::connect('/:controller/:action/*', ...)
should implicitly include the prefix.- The parameter should be available in
$this->params['lang']
or somewhere similar to be evaluated inAppController::beforeFilter()
.
- How to get
Router::url()
to automatically include the prefix in the URL, if not explicitly specified?Router::url(array('controller' => 'foo', 'action' => 'bar'))
should return/en/foo/bar
- Since
Controller::redirect()
,Form::create()
or evenRouter::url()
directly need to have the same behavior, overriding every single function is not really an option.Html::image()
for instance should produce a prefix-less URL though.
The following methods seem to call Router::url
.
Controller::redirect
Controller::flash
Dispatcher::__extractParams
viaObject::requestAction
Helper::url
JsHelper::load_
JsHelper::redirect_
View::uuid
, but only for a hash generation
Out of those it seems the Controller and Helper methods would need to be overridden, I could live without the JsHelper
. My idea would be to write a general function in AppController
or maybe just in bootstrap.php
to handle the parameter insertion. The overridden Controller and Helper methods would use this function, as would I if I wanted to manually call Router::url
. Would this be sufficient?
An easier way might be to store the chosen language in a cookie and then not have to rewrite all the URLs. You could also potentially detect the user's browser language automatically.
However, search engines would be unlikely to pickup the various languages and you'd also lose the language if someone tried to share the link.
But love the full solution you posted, very comprehensive, thanks. :-)
This is essentially all the code I implemented to solve this problem in the end (at least I think that's all ;-)):
/config/bootstrap.php
/config/core.php
/app_helper.php
/app_controller.php
/config/routes.php
This allows default URLs like
/controller/action
to use the default language (JPN in my case), and URLs like/eng/controller/action
to use an alternative language. This logic can be changed pretty easily in therouter_url_language()
function.For this to work I also need to define two routes for each route, one containing the
/:language/
parameter and one without. At least I couldn't figure out how to do it another way.rchavik from IRC suggested this link: CakePHP URL based language switching for i18n and l10n internationalization and localization
In general, it seems that overriding
Helper::url
might be the solution.