grape1
grape1

Reputation: 749

php dont return the config file content

public function loadConfig($config)
{
    if(is_file(path('config') . $config . '.php'))
    {
        return include_once path('config') . $config . '.php';
    }

}

I have the same function for loading models in the controller and everything is ok. But I cant include the config file, the path is right. If I put before the return

include_once path('config') . $config . '.php';
print_r($config_array);

It print the array value

Upvotes: 0

Views: 106

Answers (1)

Niko
Niko

Reputation: 26730

You will need to strip the "_once" (because preventing a second inclusion does not make sense in this context, it does for classes but not for config files). Furthermore, you need to either include a "return" statement in the included file or return the array, not the return value of the include function:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        include($filename);
        return $config_array;
    }

    // error handling, i.e., throw an exception ...
}

Solution using the "return" statement:

Config file:

$config_array = array( ... );
return $config_array;

Class with the config loader method:

public function loadConfig($config)
{
    $filename = path('config') . $config . '.php';
    if (is_readable($filename)) {
        return include($filename);
    }

    // error handling, i.e., throw an exception ...
}

Upvotes: 1

Related Questions