我有一个多语言网站,我想为每种语言创建一个特定的缓存文件夹。我该怎么做呢?我目前使用的是一个缓存文件夹和下面的代码。
你能帮我吗?
$lang = $CI->session->userdata('language');
$cache_path .= md5($uri).'-'.$lang;
发布于 2018-06-10 13:33:21
我找到了一个解决方案。我们必须在核心文件夹中编辑output.php
,如下所示。
public function _write_cache($output){
$CI =& get_instance();
$lang = $CI->session->userdata('language');
$path = $CI->config->item('cache_path');
$cache_path = ($path === '') ? APPPATH.'cache_'.$lang.'/' : $path;
//other code
}
public function _display_cache(&$CFG, &$URI){
$CI =& get_instance();
$lang = $CI->session->userdata('language');
$cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache_'.$lang.'/' : $CFG->item('cache_path');
//other code
}
发布于 2018-06-10 03:16:34
配置缓存文件夹的参数位于名为cache_path
的config.php
中。为了在运行时修改,我们可以使用$this->config->set_item
函数。显然,缓存文件夹切换应该在调用缓存函数之前尽可能早地在控制器函数中完成。
下面是一个示例实现,即控制器Test
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* @see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$user_language = 'french';
$this->config->set_item('cache_path', 'IS_ROOT/cache/' . $user_language . '/');
$this->output->cache(1);
$this->load->view('welcome_message');
}
}
当您运行它时,您可以在日志中看到,使用了"global“下的目录"french”是_ROOT/cache:
INFO - 2018-06-09 22:12:39 --> File loaded: /php_basedir/CodeIgniter_3_1_8/application/views/welcome_message.php
DEBUG - 2018-06-09 22:12:39 --> Cache file written: IS_ROOT/cache/french/bc3ad60292ed776397da07cac67ddd28
INFO - 2018-06-09 22:12:39 --> Final output sent to browser
DEBUG - 2018-06-09 22:12:39 --> Total execution time: 0.0138
希望能有所帮助
https://stackoverflow.com/questions/50774282
复制相似问题