cakephp 2.x路由器与i18n国际化
我使用的是CakePHP2 我很难在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:eng”。但是我想得到类似于:“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”后,该语言将消失在重定向的url中 以下是我目前的路线:cakephp 2.x路由器与i18n国际化,cakephp,cakephp-2.0,Cakephp,Cakephp 2.0,我使用的是CakePHP2 我很难在routes.php中自定义我的路由 所以在我的例子中,我有一个Post模型和一个PostsController。“索引”操作列出了所有我的帖子,每个列出的帖子都是指向“我的显示”操作的可单击链接。因此,对于posts>index,我的“标准”路径是:“localhost/cakesite/posts/index”,而对于翻译版本,它类似于:“localhost/cakesite/eng/posts/index”。相应的修改路由为:“localhost/cak
// 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代码显示什么与什么有关,但是如果你需要什么,也请告诉我我会编辑这篇文章。那么,有人知道我做错了什么吗
提前感谢您的帮助!我认为您的路线基本正确。困扰您的是URL的显示 根据您的最后一段代码,您对url的嵌入方式是使用如下数组
$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*/)
是的,修女们,就是这样!现在它工作得很好,但我必须经历很多事情才能让它工作。现在我可以说我对路线的工作原理有了更多的了解。谢谢你们的帮助!