21.10.2015 Views

1-33

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Symfony2 – Franz Jordán 2011<br />

Configuration Formats<br />

Throughout the chapters, all configuration examples will be shown in all three formats (YAML,<br />

XML and PHP). Each has its own advantages and disadvantages. The choice of which to use is up<br />

to you:<br />

YAML: Simple, clean and readable;<br />

XML: More powerful than YAML at times and supports IDE autocompletion;<br />

PHP: Very powerful but less readable than standard configuration formats.<br />

Environments<br />

An application can run in various environments. The different environments share the same PHP<br />

code (apart from the front controller), but use different configuration. For instance,<br />

a devenvironment will log warnings and errors, while a prod environment will only log errors.<br />

Some files are rebuilt on each request in the dev environment (for the developer's convenience),<br />

but cached in the prod environment. All environments live together on the same machine and<br />

execute the same application.<br />

A Symfony2 project generally begins with three environments (dev, test and prod), though<br />

creating new environments is easy. You can view your application in different environments<br />

simply by changing the front controller in your browser. To see the application in<br />

the dev environment, access the application via the development front controller:<br />

http://localhost/app_dev.php/hello/Ryan<br />

If you'd like to see how your application will behave in the production environment, call<br />

the prodfront controller instead:<br />

http://localhost/app.php/hello/Ryan<br />

If you open the web/app.php file, you'll find that it's configured explicitly to use<br />

theprod environment:<br />

$kernel = new AppKernel('prod', false);<br />

You can create a new front controller for a new environment by copying this file and<br />

changing prod to some other value.<br />

Since the prod environment is optimized for speed; the configuration, routing and Twig<br />

templates are compiled into flat PHP classes and cached. When viewing changes in<br />

the prod environment, you'll need to clear these cached files and allow them to rebuild:<br />

php app/console cache:clear --env=prod<br />

The test environment is used when running automated tests and cannot be accessed directly<br />

through the browser. See the testing chapter for more details.<br />

Environment Configuration<br />

The AppKernel class is responsible for actually loading the configuration file of your choice:<br />

// app/AppKernel.php<br />

public function registerContainerConfiguration(LoaderInterface $loader)<br />

{<br />

$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');<br />

}<br />

You already know that the .yml extension can be changed to .xml or .php if you prefer to use<br />

either XML or PHP to write your configuration. Notice also that each environment loads its own<br />

configuration file. Consider the configuration file for the dev environment.<br />

43

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!