How to edit and save custom config files in Laravel?

I am creating a simple web application in Laravel 4. I have a backend for managing the content of the applications. As part of the backend, I want to have a user interface for managing application settings. I want my config variables to be saved in the [FOLDER: /app/config/customconfig.php ] file .

I was wondering if there is any possibility in Laravel how to have a custom config file that can be managed / updated through the backend UI?

+3


source to share


3 answers


You will need to extend 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) . ';');
    }
}

      



Using:

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

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

$l->save($conf, '', 'customconfig');

      

+4


source


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);

      

+3


source


Afiak does not have built-in functions for managing config files. For this I see 2 options:

  • You can save your configuration to your database and override the default configuration at runtime with Config::set('key', 'value');

    . But keep in mind that

Configuration values ​​set at runtime are set only for the current request and do not carry over to subsequent requests. @see: http://laravel.com/docs/configuration

  • Since config files are simple php arrays, they are easy to read, manipulate and write . Therefore, with a little custom code, this should be done quickly.

In general, I would prefer the first option. Overriding configuration files can lead to some issues related to version control, deployment, automated testing, etc. But as always, it depends a lot on your project setup.

+1


source







All Articles