defines / constants in a bundle


What would be the best place within a bundle to put defines, constants, variables that affect the operation of a bundle.

For example, if a functionality of a bundle saves the images in / data / news / imgs / I want to have a route configured in case it is saved in the future, I do not have to review the bundle, and only change that define or constant or variable ... is there any configuration yml file for each bundle? which would be the best option.

asked by Carlos Garcia 10.11.2016 в 18:54

2 answers


Eye to that, that you put it in more places than you think, in the DIC, in the Kernel, etc.

Moreover, I recommend you see this excellent talk by Marc Morera:


Likewise, some best practices have been published:


Notice that it talks about getting into the parameters.yml INFRASTRUCTURE parameters:


Likewise, it tells you to set parameters in the config.yml that will be able to change with the enviroment (Environment), mainly.

And it talks to you about constants vs. configuration parameters.

And lighter, water:


Best Practice


Use constants to define configuration options that rarely change.

Bad practice:

# app/config/config.yml parameters:
    homepage.num_items: 10

Good practice:

// src/AppBundle/Entity/Post.php
namespace AppBundle\Entity;

class Post
    const NUM_ITEMS = 10;

    // ...

Personally, one of two; or would keep it in a class constant, or create a property of the class (private), and set it in the constructor of it, which can be redefined in the constructor of it (thus, even , you could redefine it in the services.yml of the Bundle, if you are going to define it as a service -eye to this, always use the definition as Proxy; Ocramious has a great library for this link -); and, of course, I would not use absolute routes, unless it was strictly necessary.

I hope it helps you.


answered by 11.11.2016 в 14:49

I would do it in the config.yml of the bundle (if you do not see what you create inside the config folder)

        ruta_imagenes: ruta

From the action that needs it then:

$rutaImagenes= $this->getParameter('ruta_imagenes'); 

and I import it into the app's config.yml in case you need them from another bundle

- { resource: "@AppBundle/Resources/config/config.yml" }
answered by 10.11.2016 в 19:03