The Prod Environment
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeOpen the .env
file in the root of our project and change this dev
environment variable to prod
.
// ... lines 1 - 17 | |
APP_ENV=prod | |
// ... lines 19 - 20 |
Tip
In your dev
environment, a listener serves our assets dynamically. But in prod
,
you have to compile them manually by running:
php bin/console asset-map:compile
If you don't see styles in prod
, or if you deploy the website to users - this is required.
When switching back to the dev
environment, you'll need to delete the public/assets/
directory in
order to start serving assets dynamically again. See
Symfony docs
for more details.
To see what changed, back at our browser, refresh. And... hey! Look at that! The web debug toolbar is gone. Now, let's try to change something in one of our templates. Open templates/main/homepage.html.twig
and, at the bottom, let's change Time
to Updated at
so it's more descriptive.
// ... lines 1 - 4 | |
{% block body %} | |
<main class="flex flex-col lg:flex-row"> | |
// ... lines 7 - 54 | |
<div> | |
// ... lines 56 - 57 | |
<p>Updated At: {{ issData.timestamp|date }}</p> | |
// ... lines 59 - 62 | |
</div> | |
// ... line 64 | |
</main> | |
{% endblock %} |
Clearing the prod Cache
If we head back and refresh...nothing changed. Why? For performance reasons, templates are cached. We made our change after the template was cached, so our browser can't see it yet. To fix this, we need to manually clear our cache. At your terminal, run:
bin/console cache:clear
To specify the environment cache we want to clear, we can add the --env=
option with the name of the environment we want to clear the cache for to the end of this command, like --env=prod
, for example:
bin/console cache:clear --env=prod
This can be helpful when you need to run a command in a specific environment that's different from the one you're currently working in. Since we're already developing in the prod
environment, this part of the command isn't necessary. If we run that... nice! The prod
environment cache was cleared successfully.
Okay, if we head back over and refresh the page again... ta da! We see "Updated at". Awesome. If you're ever working in the prod
environment and you don't see changes you've made to your templates, config files, etc. reflected in the browser, you may need to manually clear your cache.
Changing the Cache Adapter for prod Only
Right now, we're using cache.adapter.array
, which is kind of like a fake cache. We can see that in the config/packages/cache.yaml
file. A fake cache is fine for development, but when we're working in the prod
environment, we really want to use cache.adapter.filesystem
instead. Since we now know about the when@
syntax, let's leverage that. Below, say when@
, but this time, we need to set it to our prod
environment with when@prod:
. Below that, we'll repeat the same structure we see above - framework
, cache
, and app
- followed by cache.adapter.filesystem
.
// ... lines 1 - 23 | |
when@prod: | |
framework: | |
cache: | |
app: cache.adapter.filesystem |
Okay, to see this in action, we need to clear the cache again (since we're still in the prod
environment) with:
bin/console cache:clear
Back at our browser, if you watch closely, you'll see that our data is cached for about five seconds, and then... new data! It works. In our .env
file, let's change APP_ENV=prod
back to dev
. If we go back and refresh again... after every refresh... we see an HTTP request.
Next: Let's learn more about services.
Well, after changing from dev to prod I ended up with "Hey look at that: No CSS :("...
I think since asset mapper we have to manually build our assets in prod right?