joer80

$config->httpHosts Programmatically

28 posts in this topic

Got it installed manually!  Thanks!

You guys should add it.  I think it is a good way to access variables without touching the database and unnecessary creating tables.

Share this post


Link to post
Share on other sites

i came up with this solution today:

WARNING: your site/config.php should NOT be writable by your php user, so you would have to be careful about file permissions: https://processwire.com/docs/security/file-permissions/

usage:

updateConfig('/path/to/your/site/config.php', [
    'httpHosts' => ['domain1.example.com', 'domain2.example.com'],
    'dbName' => 'my_db',
    'dbUser' => 'my_db_user',
    'dbPass' => 'my_db_password',
]);

function:

/**
 * update config file
 */
function updateConfig($file, $options = []) {
    if(!is_file($file)) return 'file not found';
    $content = file_get_contents($file);

    // loop all set options
    foreach($options as $key => $val) {
        $search = '/\$config->'.$key.'(.*);/';
        
        if(is_array($val)) $replace = '$config->'.$key.' = array(\'' . implode("','", $val) . '\');';
        elseif(is_int($val)) $replace = '$config->'.$key.' = ' . $val . ';';
        elseif(is_string($val)) $replace = '$config->'.$key.' = \'' . $val . '\';';
        else return 'value must be array, integer or string';

        $content = preg_replace($search, $replace, $content);
    }

    return file_put_contents($file, $content);
}

maybe it helps someone :)

2 people like this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.