2013-04-25 89 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