0

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

1 Answer 1

1

You should generate all links and URLs within your application using HtmlHelper::link() and HtmlHelper::url()

This will make sure that your Routes are taken into account when generating URLs (Reverse routing)

For example, if you decide to define a 'friendly' URL Route /logout for /users/logout, then this:

echo $this->Html->link('Log out', array(
    'controller' => 'users', 
    'action' => 'logout'
));

Will create this link:

<a href='/logout'>Log out</a>

If you later decide to modify the 'friendly' URL Route (/sign-out) for the logout URL, then all links in your application will automatically be adjusted.

The same call to the HtmlHelper, will now output:

<a href='/sign-out'>Log out</a>

Read more on this subject here:

Reverse Routing

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.