Buy

We have two environments! That's super flexible... or I think it will be, just as soon as we figure out how the heck we can configure each environment.

Compare app.php and app_dev.php. What are the differences? Ok, ignore that big if block in app_dev.php. The important difference is a single line: the one that starts with $kernel = new AppKernel(). Hey, that's the class that lives in the app/ directory!

The first argument to AppKernel is prod in app.php:

33 lines web/app.php
... lines 1 - 22
$kernel = new AppKernel('prod', false);
... lines 24 - 33

And dev in app_dev.php:

33 lines web/app_dev.php
... lines 1 - 26
$kernel = new AppKernel('dev', true);
... lines 28 - 33

This defines your environment. The second argument - true or false - is a debug flag and basically controls whether or not errors should be shown. That's less important.

config_dev.yml versus config_prod.yml

Now, what do these dev and prod strings do? Here's the secret: when Symfony boots, it loads only one configuration file for the entire system. And no, it's not config.yml - I was lying to you. Sorry about that. No, the dev environment loads only config_dev.yml:

46 lines app/config/config_dev.yml
imports:
- { resource: config.yml }
framework:
router:
resource: "%kernel.root_dir%/config/routing_dev.yml"
strict_requirements: true
profiler: { only_exceptions: false }
web_profiler:
toolbar: true
intercept_redirects: false
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: INFO
VERBOSITY_VERY_VERBOSE: DEBUG
channels: ["!doctrine"]
console_very_verbose:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: NOTICE
VERBOSITY_VERY_VERBOSE: NOTICE
VERBOSITY_DEBUG: DEBUG
channels: ["doctrine"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
#swiftmailer:
# delivery_address: me@example.com

And in prod, the only file it loads is config_prod.yml:

28 lines app/config/config_prod.yml
imports:
- { resource: config.yml }
#framework:
# validation:
# cache: validator.mapping.cache.apc
# serializer:
# cache: serializer.mapping.cache.apc
#doctrine:
# orm:
# metadata_cache_driver: apc
# result_cache_driver: apc
# query_cache_driver: apc
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console

Ok fellow deep sea explorers, this is where things get cool! Look at the first line of config_dev.yml:

46 lines app/config/config_dev.yml
imports:
- { resource: config.yml }
... lines 3 - 46

What does it do? It imports the main config.yml: the main shared configuration. Then, it overrides any configuration that's special for the dev environment.

Check this out! Under monolog - which is the bundle that gives us the logger service - it configures extra logging for the dev environment:

46 lines app/config/config_dev.yml
... lines 1 - 13
monolog:
handlers:
main:
... lines 17 - 18
level: debug
... lines 20 - 46

By setting level to debug, we're saying "log everything no matter its priority!"

So what about config_prod.yml? No surprise: it does the exact same thing: it loads the main config.yml file and then overrides things:

28 lines app/config/config_prod.yml
imports:
- { resource: config.yml }
... lines 3 - 28

This file has a similar setup for the logger, but now it says action_level: error:

28 lines app/config/config_prod.yml
... lines 1 - 15
monolog:
handlers:
main:
... line 19
action_level: error
... lines 21 - 28

This only logs messages that are at or above the error level. So only messages when things break.

Experimenting with config_dev.yml

Let's play around a bit with the dev environment! Under monolog, uncomment the firephp line:

46 lines app/config/config_dev.yml
... lines 1 - 13
monolog:
handlers:
... lines 16 - 34
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
firephp:
type: firephp
level: info
... lines 40 - 46

This is a cool handler that will show you log messages right in your browser.

Head over to it and run "Inspect Element". Make sure that the URL will access the dev environment and then refresh. And check this out: a bunch of messages telling us what route was matched. Heck, we can even see what route was matched for our ajax call. To see this working, you'll need a FirePHP extension installed in your browser. In your app, Monolog is attaching these messages to your response headers, and the extension is reading those. We don't want this to happen on production, so we only enabled this in the dev environment.

Environments are awesome! So how could we use them only cache our markdown string in the prod environment?

Leave a comment!

  • 2016-07-01 Ilya

    Hey
    No, it's ok. Thank you

  • 2016-07-01 Victor Bocharsky

    Hey, Ilya!

    Have you installed PHP Annotation plugin already? Can I help you with installation?

  • 2016-06-30 Ilya

    Thanks, Victor
    Becouse I've got some troubles earlier whith PHP Storm (I didn't install PHP Annotation plugin.)

  • 2016-06-30 Victor Bocharsky

    Hey, Ilya!

    It's not required for other screencasts, just a usage example for this chapter. But feel free to use this if it helps you, of course.

  • 2016-06-30 Ilya

    Is it necessarily for the future tutorials\lessons Fire PHP or it just for this lesson to see messages are coming?

  • 2016-03-21 Andrew Grudin

    I catch it , thanks for your time and invaluable help!

  • 2016-03-20 weaverryan

    If it's working, don't worry about those headers. But if you're curious, open the "inspect element" on your browser (Chrome), click the network tab and then refresh. You should see a bunch of requests in this list, for the main page, CSS files etc. Select the first one (for the main page) and you should see something like what I've shown: these are the response headers sent back by Symfony. The headers are how Symfony communicates with the browser plugin :)

  • 2016-03-19 Andrew Grudin

    I have installed FirePHP4Chrome and it is working ... The fact is FirePHP for Chrome doesn't work (which was installed first by me).

    I see now in Inspect: request Object {message: "Matched route "genus_show_notes".", context: Object}

    Just one thing, I can't find in my Inspect something like request you are talking about... and line ( Headers Preview Response Cookies Timing ) as in your http://i.imgur.com/0d85kfK.png.
    Where should I look that stuff for?
    Best regards!

  • 2016-03-18 weaverryan

    Hey Andrew!

    Did you also make the change in config_dev.yml to activate this on the Symfony side? One other way to check (to see if things are configured correctly on the Symfony side) is to open your inspect, refresh, then find the request for the full page that just finished. In the first tab, you can see the "response" headers. There should be a BUNCH of weird response headers - like this: http://i.imgur.com/0d85kfK.png. If you can see that, you're setup on the Symfony side - but something isn't right with the extension.

    Cheers!

  • 2016-03-18 Andrew Grudin

    I have installed FirePHP extension in my Chrome but don't see any message telling me what route was matched when inspecting element on localhost:8000/genus/octopus (((