Skip to content
Derek Jones edited this page Jul 5, 2012 · 5 revisions

Category:Core::Deprecated

Introduction

This is a patch for the config::load() behaviour that applies to 1.3.0 to 1.3.3 of Code Igniter:

Adds a parameter, $fail_gracefully, which controls if a CI error page is displayed if the file cannot be loaded or doesn't contain the config array (if false), or if it just returns true or false depending on if it was successful or not (true). Default is to display a CI error message and halt processing of the rest of the application.

The Patch

Here is the new load() function, for system/libraries/config.php:

/**
     * Load Config File
     *
     * @access   public
     * @param   string    the config file name
     * @param   boolean  true if errors should just return false, false if an error message should be displayed
     * @return  boolean  if the file was successfully loaded or not 
     */
    function load($file = '', $fail_gracefully = false)
    {
        $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
    
        if (in_array($file, $this->is_loaded))
        {                
            return true;
        }
    
        if (!file_exists(APPPATH.'config/'.$file.EXT)) {
            if ($fail_gracefully) 
            {
                return false;
            }
            show_error('The configuration file '.$file.EXT.' does not exist.');
        }
        
        include_once(APPPATH.'config/'.$file.EXT);

        if ( ! isset($config) OR ! is_array($config))
        {
            if ($fail_gracefully) 
            {
                return false;
            }
            show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
        }
        
        $this->config = array_merge($this->config, $config);
       
        $this->is_loaded[] = $file;
        unset($config);

        log_message('debug', 'Config file loaded: config/'.$file.EXT);
        return true;
    } // END load()

Or if you prefer, a unified diff version (you can apply by saving it as 'config.patch', then running 'patch < config.patch' in the main directory (where system is a subdirectory):

--- CodeIgniter_1.3.3/system/libraries/Config.php       2006-04-03 18:54:22.000000000 -0400
+++ system/libraries/Config.php 2006-05-12 15:46:13.000000000 -0400
@@ -53,30 +53,59 @@
         *
         * @access      public
         * @param       string  the config file name
-        * @return      void
+        * @param       boolean true if errors should just return false, false if an error message should be displayed
+     * @return boolean if the file was successfully loaded or not
         */
-       function load($file = '')
+       function load($file = '', $fail_gracefully = false)
        {
                $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);

                if (in_array($file, $this->is_loaded))
                {
-                       return;
+                       return true;
                }

+               if (!file_exists(APPPATH.'config/'.$file.EXT)) {
+                       if ($fail_gracefully)
+                       {
+                               return false;
+                       }
+                       show_error('The configuration file '.$file.EXT.' does not exist.');
+               }
+
                include_once(APPPATH.'config/'.$file.EXT);

                if ( ! isset($config) OR ! is_array($config))
                {
+                       if ($fail_gracefully)
+                       {
+                               return false;
+                       }
                        show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
                }

                $this->config = array_merge($this->config, $config);

                $this->is_loaded[] = $file;
                unset($config);

                log_message('debug', 'Config file loaded: config/'.$file.EXT);
+               return true;
        }// END load()
Clone this wiki locally