codeigniter 的自定义配置文件

     2023-05-08     40

关键词:

【中文标题】codeigniter 的自定义配置文件【英文标题】:Custom config file for codeigniter 【发布时间】: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.

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

【问题讨论】:

【参考方案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

【讨论】:

使用 CodeIgniter 表单验证的自定义错误消息

】使用CodeIgniter表单验证的自定义错误消息【英文标题】:CustomerrormessageusingCodeIgniterFormValidation【发布时间】:2013-01-0519:19:30【问题描述】:我想在我的CodeIgniter表单中制作一些自定义错误消息。我试过使用$this-&gt;form_validation... 查看详情

Codeigniter domain.com/articlename.html 中的自定义 URL

】Codeigniterdomain.com/articlename.html中的自定义URL【英文标题】:CustomUrlsinCodeigniterdomain.com/articlename.html【发布时间】:2014-03-0513:07:59【问题描述】:我需要在Codeiginter中创建自定义URL以显示来自的链接domain.com/news/id/1到domain.com/article-... 查看详情

Rails - 环境配置文件中的自定义配置?

】Rails-环境配置文件中的自定义配置?【英文标题】:Rails-Customconfiginenvironmentconfigfiles?【发布时间】:2011-03-1618:14:42【问题描述】:是否可以在config/environments/*.rb或config/environment.rb文件中定义自定义配置。是的,我如何从我的... 查看详情

读取 C# 中的自定义配置文件(Framework 4.0)

】读取C#中的自定义配置文件(Framework4.0)【英文标题】:ReadcustomconfigurationfileinC#(Framework4.0)【发布时间】:2011-09-1413:48:48【问题描述】:我正在使用C#在Framework4.0下开发应用程序。在我的应用程序中,我想创建不是app.config文件... 查看详情

springboot的自定义配置

...的,此时就需要我们手动加载。接下来,将针对SpringBoot的自定义配置文件及其加载方式进行讲解。对于这种加载自定义配置文件的需求,可以使用@PropertySource注解结合@Configuration注解配置类的方式来实现。@PropertySource注解用于指... 查看详情

springboot获取配置文件的自定义参数

1、在application.properties中自定义参数spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8spring.datasource.username=rootsprin 查看详情

Codeigniter 未运行类定义 - PHP 或 Apache 配置问题

】Codeigniter未运行类定义-PHP或Apache配置问题【英文标题】:Codeigniternotrunningclassdefinitions-PHPorApacheconfigproblem【发布时间】:2011-11-0805:59:53【问题描述】:我刚刚将工作站点文件上传到了一个新服务器,我知道它可能存在php/apache配... 查看详情

无法从 Spring Boot 应用程序中的自定义 yml 文件加载配置

】无法从SpringBoot应用程序中的自定义yml文件加载配置【英文标题】:Cannotloadconfigfromcustomymlfileinspringbootapplication【发布时间】:2017-11-1608:30:55【问题描述】:我正在从我的SpringBoot服务中的application.yml加载自定义配置。我已经通... 查看详情

App.config C# 中的自定义配置部分

】App.configC#中的自定义配置部分【英文标题】:CustomConfigsectioninApp.configC#【发布时间】:2012-10-0408:32:56【问题描述】:我是C#中配置部分的初学者我想在配置文件中创建一个自定义部分。我在谷歌搜索后尝试如下配置文件:<?xm... 查看详情

如何在 codeigniter 中使用自定义字体和图标

】如何在codeigniter中使用自定义字体和图标【英文标题】:howtousecustomfontsandiconsincodeigniter【发布时间】:2016-02-1512:08:51【问题描述】:猛击所有人我想在我的视图中使用来自flaticon.com的自定义图标http://successpermis.com/css/flaticon.html... 查看详情

如何在 Laravel-4 中包含不同环境的自定义配置

】如何在Laravel-4中包含不同环境的自定义配置【英文标题】:HowtoincludecustomconfigfordifferentenvironmentinLaravel-4【发布时间】:2013-07-1408:28:11【问题描述】:$env=$app->detectEnvironment(array(\'local\'=>array(\'*.local\'),));原始配置文件可以在... 查看详情

我可以将 Maven 配置为使用快照版本的自定义内部版本号吗

】我可以将Maven配置为使用快照版本的自定义内部版本号吗【英文标题】:CaniconfiguremaventousecustombuildnumberforSnapshotversion【发布时间】:2017-10-1714:32:37【问题描述】:如果我使用mvndeploy部署快照jar文件,那么maven会根据以下内容生... 查看详情

是否可以在我的自定义技能中使用 Alexa Voice Profile

】是否可以在我的自定义技能中使用AlexaVoiceProfile【英文标题】:IsitpossibletouseAlexaVoiceProfileinmycustomskill【发布时间】:2019-02-1801:52:31【问题描述】:是否可以在我的自定义技能中使用Alexa语音配置文件?Alexa能够通过语音识别用... 查看详情

CodeIgniter 配置文件的国际化?

】CodeIgniter配置文件的国际化?【英文标题】:InternationalizationonCodeIgniterconfigfiles?【发布时间】:2010-11-2010:48:50【问题描述】:我正在尝试做类似的事情:$config[\'first_link\']=\'lang:pagination_first_link\';$config[\'prev_link\']=\'lang:pagination_p... 查看详情

CodeIgniter 路由和 404 自定义错误

】CodeIgniter路由和404自定义错误【英文标题】:CodeIgniterRoutingand404CustomErrors【发布时间】:2016-08-0415:53:29【问题描述】:我使用CodeIgniter。我使用routes.php文件根据url的结构将某些url路由到不同的文件。一切正常。然而,几个月前... 查看详情

属性的自定义配置绑定器

】属性的自定义配置绑定器【英文标题】:CustomConfigurationBinderforProperty【发布时间】:2017-06-1919:31:29【问题描述】:我在ASP.NETCore1.1解决方案中使用配置绑定。基本上,我的ConfigureServices启动部分中有一些简单的绑定代码,如下... 查看详情

codeigniter3有啥新玩意

参考技术A  Codeigniter源码托管在github,我们可以看到最新版本为3.0-dev,让我们来看看有什么新玩意。  新的VIEWPATH常量定义,可以定义views路径  错误模板(error_xxx.php)文件移到了VIEWPATH/errors目录  SHOW_DEBUG_BACKTRACE常亮定... 查看详情

使用shell,python,go来实现ansible的自定义模块(代码片段)

...的,官方基本上都已经提供了,可以说极大的提高了我们的自动化效率,但是总会有些情况无法满足我们的需求,因此我们需要学会如何去编写一个自定义的模块在下文的介绍中,会介绍下自定义模块的工作原理,分别以shell,pyt... 查看详情