Database codeigniter数据库配置以编程方式形成用户输入
有人能帮我吗 如何以编程方式配置CI数据库设置?我在OSCommerce、Joomla、Wordpress等其他系统中看到了这一点Database codeigniter数据库配置以编程方式形成用户输入,database,codeigniter,configuration,Database,Codeigniter,Configuration,有人能帮我吗 如何以编程方式配置CI数据库设置?我在OSCommerce、Joomla、Wordpress等其他系统中看到了这一点 用户需要提交一份包含数据库信息的表单。因为Joomla、Wordpress等都有类似的表单。这是不可能的。因此,如果您需要这样的机会,您必须自己创建这样的安装程序。如果你这样做了,别忘了与社区分享。因为有类似于Joomla、Wordpress等的东西。这是不可能的。因此,如果您需要这样的机会,您必须自己创建这样的安装程序。如果你这样做了,别忘了与社区分享。这实际上是
用户需要提交一份包含数据库信息的表单。因为Joomla、Wordpress等都有类似的表单。这是不可能的。因此,如果您需要这样的机会,您必须自己创建这样的安装程序。如果你这样做了,别忘了与社区分享。因为有类似于Joomla、Wordpress等的东西。这是不可能的。因此,如果您需要这样的机会,您必须自己创建这样的安装程序。如果你这样做了,别忘了与社区分享。这实际上是一个相当复杂的过程 它基于CI,有一个安装脚本,可以添加到
application/config/database.php
,这意味着它不是不可能的
MM的许可协议禁止我发布实际的安装脚本,但您应该能够创建一些有效的东西
否则,想象一下这样的情况:
// Data from user input
$db_config['hostname'] = $this->input->post('db_host');
$db_config['username'] = $this->input->post('db_user');
$db_config['password'] = $this->input->post('db_password');
$db_config['database'] = $this->input->post('db_name');
$db_config['dbdriver'] = $this->db_driver;
$db_config['dbprefix'] = $this->db_prefix;
$db_config['pconnect'] = ($this->input->post('pconnect')) ? TRUE : FALSE;
$this->CI =& get_instance();
$this->CI->load->helper('file');
$prototype = array(
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysql',
'dbprefix' => 'mojo_',
'pconnect' => TRUE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci'
);
// Now we read the file data as a string
$config_file = read_file(APPPATH.'config/database'.EXT);
// Dollar signs seem to create a problem with our preg_replace
// so we'll temporarily swap them out
$config_file = str_replace('$', '@s@', $config_file);
// Cycle through the newconfig array and swap out the data
if (count($dbconfig) > 0)
{
foreach ($dbconfig as $key => $val)
{
if ($val === 'y')
{
$val = TRUE;
}
elseif ($val == 'n')
{
$val = FALSE;
}
if (is_bool($val))
{
$val = ($val == TRUE) ? 'TRUE' : 'FALSE';
}
else
{
$val = '\''.$val.'\'';
}
$val .= ';';
// Update the value
$config_file = preg_replace("#(\@s\@db\[(['\"])".$active_group."\\2\]\[(['\"])".$key."\\3\]\s*=\s*).*?;#", "\\1$val", $config_file);
}
}
// Put the dollar signs back
$config_file = str_replace('@s@', '$', $config_file);
// Just to make sure we don't have any unwanted whitespace
$config_file = trim($config_file);
// Write the file
$fp = fopen($this->database_path, FOPEN_WRITE_CREATE_DESTRUCTIVE);
flock($fp, LOCK_EX);
fwrite($fp, $config_file, strlen($config_file));
flock($fp, LOCK_UN);
fclose($fp);
我还没有测试过这个,但我正在尝试实现类似于我自己的一个项目的东西
我希望这能让你更靠近一点。这实际上是一个相当复杂的过程 它基于CI,有一个安装脚本,可以添加到
application/config/database.php
,这意味着它不是不可能的
MM的许可协议禁止我发布实际的安装脚本,但您应该能够创建一些有效的东西
否则,想象一下这样的情况:
// Data from user input
$db_config['hostname'] = $this->input->post('db_host');
$db_config['username'] = $this->input->post('db_user');
$db_config['password'] = $this->input->post('db_password');
$db_config['database'] = $this->input->post('db_name');
$db_config['dbdriver'] = $this->db_driver;
$db_config['dbprefix'] = $this->db_prefix;
$db_config['pconnect'] = ($this->input->post('pconnect')) ? TRUE : FALSE;
$this->CI =& get_instance();
$this->CI->load->helper('file');
$prototype = array(
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'mysql',
'dbprefix' => 'mojo_',
'pconnect' => TRUE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci'
);
// Now we read the file data as a string
$config_file = read_file(APPPATH.'config/database'.EXT);
// Dollar signs seem to create a problem with our preg_replace
// so we'll temporarily swap them out
$config_file = str_replace('$', '@s@', $config_file);
// Cycle through the newconfig array and swap out the data
if (count($dbconfig) > 0)
{
foreach ($dbconfig as $key => $val)
{
if ($val === 'y')
{
$val = TRUE;
}
elseif ($val == 'n')
{
$val = FALSE;
}
if (is_bool($val))
{
$val = ($val == TRUE) ? 'TRUE' : 'FALSE';
}
else
{
$val = '\''.$val.'\'';
}
$val .= ';';
// Update the value
$config_file = preg_replace("#(\@s\@db\[(['\"])".$active_group."\\2\]\[(['\"])".$key."\\3\]\s*=\s*).*?;#", "\\1$val", $config_file);
}
}
// Put the dollar signs back
$config_file = str_replace('@s@', '$', $config_file);
// Just to make sure we don't have any unwanted whitespace
$config_file = trim($config_file);
// Write the file
$fp = fopen($this->database_path, FOPEN_WRITE_CREATE_DESTRUCTIVE);
flock($fp, LOCK_EX);
fwrite($fp, $config_file, strlen($config_file));
flock($fp, LOCK_UN);
fclose($fp);
我还没有测试过这个,但我正在尝试实现类似于我自己的一个项目的东西
我希望这能让你更靠近一点。这种方法应该很好。您需要做的就是避免在autoload.php文件中自动加载DB库,因为每次尝试加载页面时,它都会抛出一个错误
$autoload['libraries'] = array(<strike>'database',</strike> 'form_validation', 'cart','session');
然后,您可以将其扩展到所有需要DB访问的控制器,确保排除运行安装程序的控制器。这种方法应该可以。您需要做的就是避免在autoload.php文件中自动加载DB库,因为每次尝试加载页面时,它都会抛出一个错误
$autoload['libraries'] = array(<strike>'database',</strike> 'form_validation', 'cart','session');
然后,您可以将其扩展到需要DB访问的所有控制器,确保排除安装程序运行的控制器。我找到了两个解决方案,尽管其中一个尚未测试 有很多基于Codeigniter框架的项目都有一些安装程序。就像“Repox”所写的,您可以在MojoMotor中找到它,有一个core/Config.php扩展类来处理这个特定的任务,表达式引擎也有这个扩展类。可悲的是,这些都是商业许可证,不能使用 使用这种溶液的一个免费应用是电离CMS 看看这个课程: 第二个是在一个叫做“文件”的火花中。你可以在这里找到它: 明显的限制是它重写了配置文件 甚至在“codeigniter github问题1058”(google表示链接)中也有人指出这一点,但遗憾的是,开发人员决定关闭它
就我个人而言,我不明白为什么这样一个功能没有在框架中实现,它不仅可以在安装时用来编辑配置文件,还可以在控制面板中使用。我找到了两个解决方案,尽管其中一个我还没有测试过 有很多基于Codeigniter框架的项目都有一些安装程序。就像“Repox”所写的,您可以在MojoMotor中找到它,有一个core/Config.php扩展类来处理这个特定的任务,表达式引擎也有这个扩展类。可悲的是,这些都是商业许可证,不能使用 使用这种溶液的一个免费应用是电离CMS 看看这个课程: 第二个是在一个叫做“文件”的火花中。你可以在这里找到它: 明显的限制是它重写了配置文件 甚至在“codeigniter github问题1058”(google表示链接)中也有人指出这一点,但遗憾的是,开发人员决定关闭它
就我个人而言,我不明白为什么这样的功能没有在框架中实现,它不仅可以在安装时用于编辑配置文件,还可以在控制面板中使用。感谢Repox的回复。感谢Repox的回复。