phpcakephpinternationalizationcakephp-2.3cakephp-routing

cakephp don't display index action in url


I'm using the following code in my AppHelper.php to inject the language parameter into links created with the HtmlHelper

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

It's working fine but when I go to the index action the url becomes something.com/some_controller/index

if I don't override the url method then the url is just something.com/some_controller

These are my routes

Router::connect('/:language/:controller/:action/*',
                   array(),
                   array('language' => 'eng|fra'));

Router::connect('/:language/:controller',
                   array('action' => 'index'),
                   array('language' => 'eng|fra')); 

Router::connect('/:language',
                   array('controller' => 'pages', 'action' => 'display'),
                   array('language' => 'eng|fra'));

I'm calling the link method like this

$this->Html->link(__('Users'), array('controller'=>'users', 'action'=>'index'))';

and I tried it without the action parameter

$this->Html->link(__('Users'), array('controller'=>'users'))';

without the action parameter but it adds the current action if I am on a page managed by the same controller

how can I make it so that the name of the action doesn't become part of the url if it is the index action?


Solution

  • The routes are matched in the order in which you set them. Reverse the order of your Router::connect() statements and you are done. ie. First set route for /:language' then for /:language/:controller' and then /:language/:controller/:action:'

    Also overriding AppHelper::url() to include the language in generate url is not needed. Use the 'persist' key in your Router::connect() statements and it will do the job for you.