繁体   English   中英

Codeigniter:如何使用Controller访问配置文件中的多维数组?

[英]Codeigniter: How to access multidimensional arrays in config file with Controller?

为什么这不起作用? 请帮我!

我的控制器代码:

class frontend_controller extends CI_Controller {

    private $values;

    public function get_config() {

            parent::__construct(); 


        $CI = &get_instance(); //assigned the object to a variable
        $CI->config->load('example_config', TRUE); // get config file

        // get all config values
        $this->values = $CI->config->item('example_config',['frontend1']);

        $this->load->library('session');
        }



    }

模板渲染控制器:

require_once('frontend_controller.php'); 
     class frontend1_controller extends frontend_controller {


        public function __construct() {
            parent::__construct();
        }


        function template() {



            $this->load->view('template', $this->get_config());
        }

     }

配置文件:

$config['frontend1']['base_url'] = "http://www.test.com/";
$config['frontend1']['login_error_url'] = "www.test.com/login_failed";
$config['frontend1']['login_success_url'] = "www.test.com/login_success";
$config['frontend1']['js'] = "www.test.com/js/file.js";
$config['frontend1']['css'] = "www.test.com/css/style.css";
$config['frontend1']['my_title'] = ' Test123' ;
$config['frontend2']['base_url'] = "www.test.com";
$config['frontend2']['login_error_url'] = "http://www.test.com/login_failed";
$config['frontend2']['login_success_url'] = "http://www.test.com/login_success";
$config['frontend2']['js'] = "http://www.test.com/js/file.js";
$config['frontend2']['css'] = "http://www.test.com/css/style.css";
$config['frontend2']['my_title'] = ' Test123' ;

在模板中:

<p><?php echo $base_url ?></p>

我想读取配置文件,但只读取frontend1-arrays。 我怎么做?

请不要用括号写;

$this->values = $CI->config->item('example_config', 'frontend1');

您可以像这样阅读所有这些内容:

       $data = $this->config->item('frontend1');

       print_r($data); 

或者你可以像这样逐一阅读:

$my_title = $this->config->item('my_title','frontend1');
echo $my_title;

配置文件:

$config['permissions'] = [
           [
              'name' => 'Xem',
              'key' => 'view',
              'val' => 0
           ],
           [
              'name' => 'Thêm',
              'key' => 'add',
              'val' => 0
           ]
        ];

模板文件:

echo '<pre>';
print_r($this->config->item('permissions'));
echo '</pre>';
die;

结果:

Array(
  [0] => Array
    (
        [name] => View
        [key] => view
        [val] => 0
    )

   [1] => Array
    (
        [name] => Add
        [key] => add
        [val] => 0
    )
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM