【问题标题】:Custom config file for codeignitercodeigniter 的自定义配置文件
【发布时间】:2014-01-15 15:32:32
【问题描述】:

对 CodeIgniter 非常陌生,正在尝试创建自定义配置文件以将特殊变量加载到我的应用程序中。

application/config/ 中,我创建了custom.php 并将以下代码放入该文件中:

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

$gender = array ('male','female'); 

?>

然后我打开application/config/autoload 并更改了以下代码:

$autoload['config'] = array();

/* TO: */ 

$autoload['config'] = array('custom');

我刷新我的应用程序并看到这个错误:

Your application/config/custom.php file does not appear to contain a valid configuration array.

我打开了一些默认配置文件,但没有看到配置数组?我做错了什么?

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    使用

    $config['gender']= array ('male','female');
    

    而不是

    $gender = array ('male','female');
    

    用于获取配置项

    $this->config->item('item_name');
    

    其中item_name 是您要检索的$config 数组索引。

    文档:CodeIgniter User Guide 2.xCodeIgniter User Guide 3.x

    【讨论】:

      【解决方案2】:

      创建自定义配置文件: 在“application/config/”下添加一个名为“custom_config.php”(或给出任何名称)的新文件并添加以下代码

      <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
      //adding config items.
      $config['gender'] = array('female', 'male');
      

      加载自定义配置文件 :- 创建自定义配置文件后,我们需要加载它来获取它的项目。对于加载自定义配置,我们有两种方法

      *** 手动加载:- 我们可以像手动加载控制器/模型中的配置文件

      $this->config->load('custom_config'); //or instead your file name.
      

      *** Autoloading :- 对于自动加载配置文件,转到“application/config/autoload.php”并在 $autoload['config'] 中添加代码

      $autoload['config'] = array('custom_config'); //or instead your file name.
      

      【讨论】:

      • $gender = $this->config->item('gender'); // 返回数组
      • 另外,你可以使用 $this->load->config('custom_config') 来加载你的配置文件。
      【解决方案3】:

      CodeIgniter 3

      第一步:创建自定义配置文件

      /path/to/codeigniter/application/config/custom.php

      <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
      
      $config['server'] = array (
          'host'      => 'example.com',
          'public_ip' => '93.184.216.34'
          );
      ?>
      

      第二步:加载自定义配置文件

      这可以在模型或控制器中完成。

      // load the configuration file
      $this->config->load('custom');
      
      // retrieve the content of a specific configuration
      $server = $this->config->item('server');
      
      // $server is now set
      echo $server['host']; // example.com
      

      【讨论】:

        猜你喜欢
        • 2016-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 2015-05-15
        相关资源
        最近更新 更多