nipec
nipec

Reputation: 67

Symfony write to parameters.yaml from controller

I want to write an integer value from a controller to parameters.yaml. Is that even possible?

Example:

parameters.yaml

parameters:
    # ...
    counter: 13

SomeController.php

class SomeController
{
    public function indexAction()
    {
        $counter = $this->getParameter('counter');
        $counter++;
        // now save new counter value to parameters.yaml !??
    }
}

Upvotes: 2

Views: 1553

Answers (2)

Confidence
Confidence

Reputation: 2303

Parameters are generally fixed values. So A better approach is probably writing into an individual yaml file:

http://symfony.com/doc/current/components/yaml/introduction.html

use Symfony\Component\Yaml\Dumper;


const MY_PARAM=13;

//manipulate and do your thing....
$array=['my_param'=>self::MY_PARAM++];



$dumper = new Dumper();

$yaml = $dumper->dump($array);

file_put_contents('/path/to/file.yml', $yaml);

Then you read the file wherever you need it in your application.

use Symfony\Component\Yaml\Parser;

$yaml = new Parser();

$value = $yaml->parse(file_get_contents('/path/to/file.yml'));

Upvotes: 3

tannana
tannana

Reputation: 121

Parameters.yml must contain only fixed configuration values ! You should store your counter in database or (i don't like this) in txt file.

But if you really want to edit it. You have to parse the file and search / replace the line ... It's really a bad practice !

Upvotes: 1

Related Questions