2

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 !??
    }
}

2 Answers 2

3

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'));
Sign up to request clarification or add additional context in comments.

Comments

1

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 !

1 Comment

But it's just a single value?! Doesn't make much sense to me to create a database table just for that. I think I will put it into a txt file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.