我想在Codeigniter中使用包含常量的新文件,因此我创建了文件/config/labels.php
当我尝试使用$this->config->load('labels');
在我的控制器中加载它时它会抛出
application/config/labels.php file does not appear to contain a valid configuration array.
然而,当我把代码放在constants.php文件中时,一切都运行良好。
labels.php
<?php
define('CLI_CIVILITE','Civilité');
define('CLI_NOM','Nom');
define('CLI_PRENOM','Prenom');
答案 0 :(得分:8)
配置文件应包含数组$config
这就是它抛出错误的原因。
当config类加载配置文件时,它会检查是否设置了$config
。
如果不是,它将引发错误。
据我所知,没有功能可以使用自定义常量加载您自己的文件。
截至目前,您必须将这些常量添加到application/config/constants.php
答案 1 :(得分:2)
在常量文件中,定义如下变量:
$ORDER_STATUS = array(
'0' => 'In Progress',
'1' => 'On Hold',
'2' => 'Awaiting Review',
'3' => 'Completed',
'4' => 'Refund Requested',
'5' => 'Refunded');
然后,在控制器中:
function __construct()
{
$this->config->load('$ORDER_STATUS');
}
答案 2 :(得分:0)
在您的配置示例中写入并保存为banned_idcard.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config ['banned_idcard'] = array (
'23104013',
'2010201103',
'11106062',
);
在你的Controoler中
<?php
function __construct () {
$banned_idcards = $this->config->load('banned_idcard');
}
答案 3 :(得分:0)
/config/labels.php
中:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config = array(
'CLI_CIVILITE' => 'Civilité',
'CLI_NOM' => 'Nom',
'CLI_PRENOM' => 'Prenom'
);
在您的控制器中:
$this->config->load('labels');
var_dump((array)$this->config); //show all the configs including those in the labels.php