我在使用CakePHP (2.3)的CacheHelper时遇到了麻烦。这真的是一个强大的工具,但它不太适合我正在做的事情。
我有一个国际化的网站,语言可以根据用户的喜好设置,也可以通过URL参数(lang:xx)“强制”设置。
因此,缓存的页面"controller/action/yy“可以与"controller/action/yy/lang:xx”是同一页。最糟糕的是,"controller/action/yy“可以用英语、法语或其他语言缓存。
有没有办法更改缓存文件的名称(前缀显然不是解决方案)(这样通过添加用户的首选语言,"controller/action/yy/lang:xx“将始终被缓存为”controller/action/yy/lang:xx“)?
提前感谢!塞巴斯蒂安
发布于 2013-05-02 02:03:08
您可以在beforeRender中更改文件的前缀:
Configure::write('Cache.viewPrefix', 'YOURPREFIX');您可以从params或session (取决于您的应用程序)获取前缀。我不明白为什么前缀没有用。每页的每种语言都有一个文件。类似于"eng_my_action“文件。如果您只想保存一个文件并将其翻译为用户语言,这是没有意义的。因为视图已经做到了这一点(仅仅是数据的解析器)。
字体:
http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html#using-the-helper (查找2.3版中的新功能)
好的,我会试一下awnser,让你明白:
public function beforeRender(){
$lang = isset($this->params["named"]["lang"]) ? $this->params["named"]["lang"] : "eng"; // verify if is the default language(eng) or is in params
if($this->Auth->user("lang")){
$lang = $this->Auth->user("lang"); // This is a example how to take the default language from a user. You need to change it to your app.
}
Configure::write('Cache.viewPrefix', $lang);
}https://stackoverflow.com/questions/16315090
复制相似问题