Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit and save custom config files in Laravel?

I am creating simple web application in Laravel 4. I have backend for managing applications content. As a part of backend i want to have UI to manage applications settings. I want my configuration variables to be stored in file [FOLDER: /app/config/customconfig.php].

I was wondering if there's any possibility in Laravel how to have custom config file, which can be managed/updated thru backend UI?

like image 891
scorpion909 Avatar asked Sep 07 '14 14:09

scorpion909


3 Answers

Based upon @Batman answer with respect to current version (from 5.1 to 6.x):

config(['YOUR-CONFIG.YOUR_KEY' => 'NEW_VALUE']);
$text = '<?php return ' . var_export(config('YOUR-CONFIG'), true) . ';';
file_put_contents(config_path('YOUR-CONFIG.php'), $text);
like image 193
Nicholas Vasilaki Avatar answered Sep 30 '22 17:09

Nicholas Vasilaki


You'll have to extend the Fileloader, but it's very simple:

class FileLoader extends \Illuminate\Config\FileLoader
{
    public function save($items, $environment, $group, $namespace = null)
    {
        $path = $this->getPath($namespace);

        if (is_null($path))
        {
            return;
        }

        $file = (!$environment || ($environment == 'production'))
            ? "{$path}/{$group}.php"
            : "{$path}/{$environment}/{$group}.php";

        $this->files->put($file, '<?php return ' . var_export($items, true) . ';');
    }
}

Usage:

$l = new FileLoader(
    new Illuminate\Filesystem\Filesystem(), 
    base_path().'/config'
);

$conf = ['mykey' => 'thevalue'];

$l->save($conf, '', 'customconfig');
like image 30
Antonio Carlos Ribeiro Avatar answered Sep 30 '22 17:09

Antonio Carlos Ribeiro


I did it like this ...

config(['YOURKONFIG.YOURKEY' => 'NEW_VALUE']);
$fp = fopen(base_path() .'/config/YOURKONFIG.php' , 'w');
fwrite($fp, '<?php return ' . var_export(config('YOURKONFIG'), true) . ';');
fclose($fp);
like image 23
Batman Avatar answered Sep 30 '22 17:09

Batman