【问题标题】:codeigniter database configuration programmatically form user inputcodeigniter 数据库配置以编程方式形成用户输入
【发布时间】:2011-11-03 11:21:11
【问题描述】:

谁能帮帮我?

如何以编程方式配置 CI 数据库设置?我在 OSCommerce、Joomla、Wordpress 等其他系统中看到了这一点。

用户需要提交带有数据库信息的表单。

【问题讨论】:

    标签: database codeigniter configuration


    【解决方案1】:

    我找到了两种解决方案,虽然其中一种我还没有测试过。

    有很多基于 Codeigniter 框架的项目都有某种安装程序。就像“Repox”写的,你可以在 MojoMotor 中找到它,有一个 core/Config.php 扩展类来处理这个特定的任务,Expression Engine 也有这个。可悲的是,这些都已获得商业许可,无法使用。

    使用这种解决方案的一个免费应用程序是 Ionize CMS。

    看看这个类:https://github.com/ionize/ionize/blob/master/install/class/Config.php

    第二个是在名为“文件”的火花中。你可以在这里找到它:http://getsparks.org/packages/files/versions/HEAD/show

    明显的限制是它重写了配置文件。

    即使在“codeigniter github issue 1058”(google that for link)中也有所指出,但遗憾的是开发人员决定关闭它。

    我个人不明白为什么框架中没有实现这样的功能,它不仅可以用于在安装时编辑配置文件,还可以在控制面板中使用。

    【讨论】:

      【解决方案2】:

      因为有 no automated CI installer 就像 Joomla、Wordpress 等一样,所以这是不可能的。因此,如果您需要这样的机会,则必须自己创建这样的安装程序。如果你这样做了,别忘了与社区分享。

      【讨论】:

        【解决方案3】:

        这实际上是一个相当复杂的过程。

        基于 CI 的MojoMotor 有一个安装脚本,可以添加到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);
        

        我没有对此进行测试,但我正在尝试实现类似于我自己的项目之一的东西。

        我希望这能让你更接近一点。

        【讨论】:

        • 感谢 Repo 的回复。
        【解决方案4】:

        这种方法应该没问题。您需要做的就是避免在 autoload.php 文件中自动加载数据库库,否则每次尝试加载页面时都会引发错误。

        $autoload['libraries'] = array(<strike>'database',</strike> 'form_validation', 'cart','session');
        

        然后您可以拥有一个 MY_Controller,它具有:

        function __construct()
        {
            $this->load->database();
        }
        

        然后,您可以将其扩展到所有需要数据库访问权限的控制器,确保排除运行设置的控制器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-27
          • 1970-01-01
          • 2018-10-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多