Symfony Global Parameters


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() );

 

Advertisements

About Alvin Bunk
Hi, I'm a software developer at Taft College.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: