Welcome Guest, Not a member yet? Register   Sign In
Problem with config->load() and third_party packages
#1

[eluser]Unknown[/eluser]
Hi there

I found that when loading a config in a third_party subdirectory (/third_party/myapp/config/myconfig.php), CI would also try to load from one directory down, i.e. /third_party/config/myconfig.php.

This results in two errors - one on the include and one one the valid config check.

The config->load method is a bit messy, so I've fixed it for myself by encapsulating the loading with an "if file_exists($file_path) {" just before the include.

My method now looks like this:
Code:
// --------------------------------------------------------------------
    /**
     * Load Config File
     *
     * @access    public
     * @param    string    the config file name
     * @param   boolean  if configuration values should be loaded into their own section
     * @param   boolean  true if errors should just return false, false if an error message should be displayed
     * @return    boolean    if the file was loaded correctly
     */
    function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
    {
        $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
        $found = FALSE;
        $loaded = FALSE;

        foreach ($this->_config_paths as $path)
        {
            $check_locations = defined('ENVIRONMENT')
                ? array(ENVIRONMENT.'/'.$file, $file)
                : array($file);

            foreach ($check_locations as $location)
            {
                $file_path = $path.'config/'.$location.EXT;

                if (in_array($file_path, $this->is_loaded, TRUE))
                {
                    $loaded = TRUE;
                    continue 2;
                }

                if (file_exists($file_path))
                {
                    $found = TRUE;
                    break;
                }
            }

            if ($found === FALSE)
            {
                continue;
            }
            if (file_exists($file_path)) {
                include($file_path);
            
                if ( ! isset($config) OR ! is_array($config))
                {
                    if ($fail_gracefully === TRUE)
                    {
                        return FALSE;
                    }
                    show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
                }
                
                if ($use_sections === TRUE)
                {
                    if (isset($this->config[$file]))
                    {
                        $this->config[$file] = array_merge($this->config[$file], $config);
                    }
                    else
                    {
                        $this->config[$file] = $config;
                    }
                }
                else
                {
                    $this->config = array_merge($this->config, $config);
                }
                
                $this->is_loaded[] = $file_path;
                unset($config);
                
                $loaded = TRUE;
                log_message('debug', 'Config file loaded: '.$file_path);
            }
        }

        if ($loaded === FALSE)
        {
            if ($fail_gracefully === TRUE)
            {
                return FALSE;
            }
            show_error('The configuration file '.$file.EXT.' does not exist.');
        }

        return TRUE;
    }
#2

[eluser]ilNotturno[/eluser]
Hi,
I had the same problem, but i found another solution, because the load function already check if the file exists or not.

The solution is to move variables:
Code:
$found = FALSE;
$loaded = FALSE;

From outside to inside "foreach" statement:

ERROR
Code:
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
    {
        $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
        $found = FALSE;
        $loaded = FALSE;

        foreach ($this->_config_paths as $path)
        {
            $check_locations = defined('ENVIRONMENT')
                ? array(ENVIRONMENT.'/'.$file, $file)
                : array($file);


GOOD!
Code:
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
    {
        $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
        foreach ($this->_config_paths as $path)
        {
            $found = FALSE;
            $loaded = FALSE;

            $check_locations = defined('ENVIRONMENT')
                ? array(ENVIRONMENT.'/'.$file, $file)
                : array($file);

Bye bye! Have a good coding Smile




Theme © iAndrew 2016 - Forum software by © MyBB