Question

So here's my problem. I have a website that we're currently translating in french using cakephp. When I am on a page with a url such as "www.mydomain.com/eng/store/view/1" The url I see in the french language link is "www.mydomain.com/fre/store/view/".

Here's my code in my routes.php that is related to the store page

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

In my app_helper.php I have

function url($url = null, $full = false) {
        if(!isset($url['language']) && isset($this->params['language'])) {
          $url['language'] = $this->params['language'];
        }     

        return parent::url($url, $full);
    }

So my question is: what should I do to make my link keep the last params of the product when switching language?

Note: The link works perfectly for pages with only a controller and an action in the url.

Was it helpful?

Solution

I have found the actual problem why my parameters were not following in the language link. I'm not entirely sure this is the best option, but it works for me.

When adding the links for the language instead of having :

echo $this->Html->link('English', array('language'=>'eng'))

I am using

echo $this->Html->link('English', array('language'=>'eng')+$this->params['pass']);

Looking to see if there was a routing issue was not entirely useless as I have found many flaw that made other pages that would not go to the correct url, mainly the pages under the pages folder. Adding

Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/:language/pages/*', array('controller' => 'pages', 'action' => 'display'), array('language' => '[a-z]{3}'));

Did the trick for these.

OTHER TIPS

This works in all the cases and passing variable:

Router::connect('/action/**', array('controller' => 'controller_name', 'action'=>'action_name'));

Here if you want to limit your url and add variable to your url =>e.g. controller/action/variable

 Router::connect('/action/:id', array('controller' => 'controller_name', 'action'=>'action_name')
                              , array('pass'=>array('id')));

Here is reference

hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top