首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在CodeIgniter中使用多个缓存文件夹

如何在CodeIgniter中使用多个缓存文件夹
EN

Stack Overflow用户
提问于 2018-06-09 20:25:05
回答 2查看 994关注 0票数 0

我有一个多语言网站,我想为每种语言创建一个特定的缓存文件夹。我该怎么做呢?我目前使用的是一个缓存文件夹和下面的代码。

你能帮我吗?

$lang = $CI->session->userdata('language');
$cache_path .= md5($uri).'-'.$lang;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 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
        }
票数 0
EN

Stack Overflow用户

发布于 2018-06-10 03:16:34

配置缓存文件夹的参数位于名为cache_pathconfig.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

希望能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50774282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档