7

Let's say I have a simple traditional contact form on my site and I would like it to use the subject "Test: (subject_field value)" in dev environment and "(subject_field_value)" in prod environment when sending e-mail. Is there a way to define a variable called "subject_prefix" in config_dev.yml and config_prod.yml and then just use something like $this->get('config')->get('subject_prefix')? I would expect that call to return "Test: (subject_field value)" in dev environment and "(subject_field_value)" in prod environment.

Ohas
  • 1,887
  • 4
  • 21
  • 29

3 Answers3

8

The best to do is :

In the config.yml

parameters:
    url: domain.com

In the controller :

$value = $this->container->getParameter('url');

Hope it helps.

In Symfony 2.7+:

$value = $this->getParameter('url');
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
Mirza Selimovic
  • 1,669
  • 15
  • 18
4

See the How to expose a Semantic Configuration for a Bundle cookbook article.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
0

If you do not want to clutter your "parameters.yml" and do want to store it in the config.yml and config_dev.yml look at my answer here:

How do I read configuration settings from Symfony2 config.yml?

The one that contains the two approaches:

  • FIRST APPROACH: Separated config block, getting it as a parameter
  • SECOND APPROACH: Separated config block, injecting the config into a service

Hope this helps!

Community
  • 1
  • 1
Xavi Montero
  • 9,239
  • 7
  • 57
  • 79