我使用this solution作为我的CakePHP 2.3网站的i18n。CakePHP获取当前网址,不含语言前缀
当用户在这个网址:
<a href="/eng<?php echo $this->here;?>">English</a>
但是,当用户在这个网址:example.com/fre/myController/myAction/param1/param2
我example.com/myController/myAction/param1/param2
我想给链接example.com/eng/myController/myAction/param1/param2
这与我的视图文件中的代码运行良好不能链接到这个网址:example.com/eng/myController/myAction/param1/param2
我可以得到这个完整的URL:
fullURL = Router::url($this->here, true);
$strippedURL = str_replace("http://example.com/fre/myController/myAction/param1/param2","myController/myAction/param1/param2",$fullURL)
但我需要为每种语言做到这一点。或者我可以从$fullURL
中删除前22个字符。但他们似乎不是很好的解决方案。 你有什么建议吗?
编辑:在我的助手/查看文件我用这个:
function getRelURL() {
$controller = $this->request->params['controller'];
$action = $this->request->params['action'];
$URL = "/".$controller."/".$action."/";
foreach ($this->request->params['pass'] as $p) {
$URL .= urlencode($p)."/";
}
}
我会很高兴,如果你能推荐更好的选择。
我想我会计数斜杠和剥离所需的斜线前的一切。 – noslone 2013-04-11 07:08:31
要使用您的路由(和**反向路由**),您应该*不*使用URL为字符串生成链接,但*始终*使用数组表示法;例如'$ this-> Html-> link('home',array('controller'=>'pages','action'=>'view','home'));'。如果您想切换到另一种语言,请将'lang'键添加到阵列中,例如'array(........,'lang'=>'fre')'也读:[静态和动态内容国际化,路由和交换](http://bakery.cakephp.org/articles/ kicaj/2013/1月27日/ internationalization_with_static_and_dynamic_content_routing_and_switching) – thaJeztah 2013-04-11 18:52:30