Introduction
It is often desirable to be able to use a global parameter in a Symfony controller. For example, instead of having to define a customer support email multiple times in various controllers, you can define the value of the email in the app/config/parameters.yml file and then get that value in your controller. This blog describes how to do that.
Parameters YAML File
First, go into your app/config/parameters.yml file and add the parameter that you need to use. In this example, I will call the parameter “support_email”, and the value of it will be “support@mycompany.com”.
# app/config/parameters.yml parameters: ... support_email: support@mycompany.com ...
Controller PHP File
Now in your Controller file you can use the above created parameter by simply using the getParameter(‘param’) method. Below, I show creating a SwiftMessage and setting the “To:” field of the email to the global parameter of the support email.
$message = \Swift_Message::newInstance() ->setSubject( 'Support Email' ) ->setFrom( $form->get('user_email')->getData() ) ->setTo( $this->getParameter('support_email') ) ->setBody( $form->get('email_content')->getData() );