简体   繁体   中英

Way from config.yml to Symfony controller

I have question about Symfony container. I tried to find some informations about how variables are returned.

For example, config.yml to my controller, where I want get this value.

Question: config.yml

my_var: 12345

Controller: DefaultController.php

public function testAction(){

    die($this->get('my_var'));

}

How does the controller get the value of my_var ?

Your custom parameter has to live in the parameters domain of your config.yml

# config.yml
parameters:
    locale: en
    my_var: 12345

Then you can use the container method getParameter() inside your code. That is if the container is available.

$this->container->getParameter('my_var')

Inside a class extending Controller.php (or any other class implementing ContainerInterface) you can simply write

$this->getParameter('my_var') which in turn calls the method above inside the super class.

Introduction to parameters

Best practices for configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM