I implemented the solution at this article.
"beforeFilter" and "_setLanguage" works fine. If URL has language parameter, I can successfully set cookies/session variables. And use it between controllers.
That solution also includes adding url() function to AppHelper.
class AppHelper extends Helper {
function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}
return parent::url($url, $full);
}
}
But I have many URLs in my View files that are written without using HtmlHelper. Like this:
// myfile.ctp
<a href="/mypage/">click me plase<a>
So it seems like AppHelper::url solution doesn't fix my issue. What would be better alternative to add language prefix to URLs?
I thought defining a global variable like this:
// AppController::_beforeFilter()
if ($this->request->params['language'] != "")
{
Configure::write('URLprefix', '/'.$this->request->params['language']);
} else {
Configure::write('URLprefix', '');
}
Then change view file like this:
// myfile.ctp
<a href="<?php echo URLprefix; ?>/mypage/">click me plase<a>
But it doesn't seem a good way. Is there a better way to add prefix to URLs. Or should I add to all links by hand ?
Related:
Adding a prefix to every URL in CakePHP
CakePHP 2.x i18n route
CakePHP 2.1 URL Language Parameter