Database 从数据库而不是配置文件读取CodeIgniter配置值
您可能知道,当您使用CI创建新项目时,您必须在config/config.php中手动输入基本url、加密密钥。我正试图克服这一点,因此正在寻找一种方法,从数据库中读取这些值,而不是——为客户进行安装,并且设置时间总体上减少了很多 客户无法编辑PHP文件的变量,但在一些指导下,很可能能够输入基本url并由系统自动填写加密密钥 有没有办法做到这一点 当然!添加并通过该文件设置这些配置值Database 从数据库而不是配置文件读取CodeIgniter配置值,database,codeigniter,config,Database,Codeigniter,Config,您可能知道,当您使用CI创建新项目时,您必须在config/config.php中手动输入基本url、加密密钥。我正试图克服这一点,因此正在寻找一种方法,从数据库中读取这些值,而不是——为客户进行安装,并且设置时间总体上减少了很多 客户无法编辑PHP文件的变量,但在一些指导下,很可能能够输入基本url并由系统自动填写加密密钥 有没有办法做到这一点 当然!添加并通过该文件设置这些配置值 config/hooks.php $hook['pre_controller'][] = array( 'cl
config/hooks.php
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
hooks/myotherclass.php
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
因为pre_控制器
无法工作,因为它在初始化$CI=&get_instance()时将返回NULL值代码>
要解决此问题:
将挂钩点从pre\u controller
更改为post\u controller\u构造函数
修改源:
config/hooks.php
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
在hooks/myotherclass.php中
$hook['pre_controller'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
$hook['post_controller_constructor'][] = array( 'class' => 'MyOtherClass',
'function' => 'MyOtherfunction',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks');
<?
class MyOtherClass {
function MyOtherfunction() {
$CI =& get_instance();
$row = $CI->db->get_where('configs', array('site_id' => 1))->row();
$CI->config->set_item('base_url', $row->base_url);
}
}
Zar,由于版本2,您根本不必在配置文件中输入基本URL。此外,如果没有加密密钥,您无法启动会话,因此您可以使用不需要会话/登录的安装脚本将值写入文件,这意味着您可能根本不需要从数据库读取配置。我想缓存这些值会提高性能?谢谢是的,但它基本上只是在每个页面加载上运行的一个简单查询。我怀疑你是否会达到值得缓存的程度,因为网站的数量可能不会增加数百万?:)值得将获得的结果保存为全局值,以便您可以将其重新用于站点的其他部分。@Zar:用户保存这些值后,您应该立即将其直接写入文件/缓存。您根本不需要DB来完成此操作。如果您想在controller中使用这些配置设置,则post controller是无用的。因为在您的方法中,它在完成控制器代码的执行之前不会设置配置。