0
votes

I'm using cakephp 2

I'm having a hard time to customize my routes in routes.php.

So in my case, I have a Post model and a PostsController. The index action lists all my post and each listed post is a clickable link that goes to my show action. So my "standard" route for posts>index is: "localhost/cakesite/posts/index" and for the translated version it's like: "localhost/cakesite/eng/posts/index". The corresponding modified routes are: "localhost/cakesite/news" and "localhost/cakesite/eng/news".

Now for the show action it's slightly different because I need to pass some parameters such as the slug and the id, so it looks like this without modification of its route: "localhost/cake/posts/show/75/language:eng". But I would like to get something like: "localhost/cakesite/news/slug-id" and the translated version: "localhost/cakesite/eng/news/slug-id". These are both routes that I can't get done. This one: "localhost/cakesite/news/slug-id" is working but when I have the pointer over the link I get: "localhost/cake/posts/show/75" but once I click on it, it redirects me on the right url which appears in my browser like: "localhost/cake/news/slug-75". That's odd to see that on mouse-over I have a different result with the redirected url. Also when my locale is set on a language, mouse-over: localhost/cake/posts/show/75/language:eng" and after clicked: "localhost/cake/news/slug-75", the language disappear in the redirected url.

So here are my routes so far:

// News index
Router::connect('/news',
    array('controller' => 'posts', 'action' => 'index')
    );
Router::connect('/:language/news',
    array('controller' => 'posts', 'action' => 'index'),
    array('language' => '[a-z]{3}','persist'=>array('language'))
    );

// News show
Router::connect('/news/:slug-:id',
    array('controller' => 'posts', 'action' => 'show'),
    array('pass' => array('id', 'slug'),
        'id' => '[0-9]+',
        'slug' => '[a-z0-9\-]+')
    );
Router::connect('/:language/news/:slug-:id',
    array('controller' => 'posts', 'action' => 'show'),
    array('pass' => array('slug','id'), 'language' => '[a-z]{3}', 'slug' => '[a-z0-9\-]+', 'id' => '[0-9]+')
    );

AppHelper

class AppHelper extends Helper {

    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);
    }
}

The actual link from index to show

$lang=Configure::read('Config.language');
        echo $this->Html->link(
            $this->Html->tag('h1', $v['name_'.$lang], array('class' => 'news_titre')).' '.
            $this->Html->tag('span', $v['created'], array('class' => 'news_date')).' '.
            $this->Html->tag('div', '', array('class' => 'clear_float')).' '.
            $this->Html->tag('span', $this->Html->image("news/".$v['photo'], array( "alt" => $v['name_'.$lang])), array('class' => 'news_thumb')).' '.
            $this->Html->tag('p', $this->Text->truncate(strip_tags($v['content_'.$lang]), 297, array('ellipsis' => '...', 'exact' => false)), array('class' => 'news_contenu')),
            array('action' => 'show',$v['id']), array('class' => 'news_box', 'escape' => false));

I have put the aboce codes to show what's connected with what but if you need anything alse let me know I will edit the post. So, does anybody have an idea of what I'm doing wrong?

Thanks in advance for your help!

1

1 Answers

0
votes

I think your routes are mostly correct. What is bothering you is the display of urls.

Acording to your last bit of code, the way you're ensambling your url is with an array like

$this->Html->tag( /* etc */, 
                  /*here's the redirection*/ array('action'=>'show', $id), /*etc*/)

Try changing that array to this

$this->Html->tag( /* etc */, 
                  /*here's the redirection*/ array('action'=>'show', 
                                                   'id' => $id,
                                                   'slug' => $slug,
                                                   'language' => $language), /*etc*/)