2013-04-25 90 views
0

我正在使用cakephp 2cakephp 2.x路由器與i18n國際化

我很難在routes.php中自定義我的路線。

所以在我的情況下,我有一個Post模型和一個PostsController。該索引操作列出了我的所有帖子,每個列出的帖子都是一個可點擊的鏈接,轉到我的節目操作。因此,對於posts> index我的「標準」路線是:「localhost/cakesite/posts/index」,翻譯版本就像「localhost/cakesite/eng/posts/index」。相應的修改路線是:「localhost/cakesite/news」和「localhost/cakesite/eng/news」。

現在對於show動作有些不同,因爲我需要傳遞一些參數,例如slug和id,所以它看起來像這樣,而不修改其路徑:「localhost/cake/posts/show/75/language :主機」。但我想得到像「localhost/cakesite/news/slug-id」和翻譯版本:「localhost/cakesite/eng/news/slug-id」。這些都是我無法完成的路線。這一個:「localhost/cakesite/news/slug-id」正在工作,但是當我將指針指向鏈接時,我得到:「localhost/cake/posts/show/75」,但是一旦我點擊它,就會重定向到在我的瀏覽器中顯示的正確url:「localhost/cake/news/slug-75」。奇怪的是,看到鼠標懸停後,我對重定向的url有不同的結果。此外,當我的語言環境設置爲語言,鼠標懸停:localhost/cake/posts/show/75/language:eng「,點擊後:」localhost/cake/news/slug-75「,語言在重定向中消失。網址

因此,這裏有我的路線至今:

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

從指數的實際鏈接顯示

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

我已經把aboce代碼顯示做什麼用什麼連接,但如果你需要什麼ALSE讓我知道我將編輯的職位。那麼,有沒有人知道我做錯了什麼?

在此先感謝您的幫助!

回答

0

我認爲你的路線大多是正確的。什麼是困擾你的是顯示網址。

Acording到的代碼的最後一位,你ensambling您的網址的方式是像

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

數組試着改變該數組這個

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

是修女,原來如此!現在它運行良好,但我必須經歷很多事情才能使其工作。現在我可以說我更瞭解路線的運作方式。謝謝你的幫助! – 2013-04-28 00:23:01