We absolutely need to cache our markdown processing. But what if we need to tweak
how the markdown renders? In that case, we don't want caching. Could we somehow
disable caching in the dev environment only?

Yes! Copy the doctrine_cache from config.yml and paste it into config_dev.yml.
Next, change type from file_system to array:

51 lines app/config/config_dev.yml

... lines 1 - 46

doctrine_cache:

providers:

my_markdown_cache:

type:array

The array type is basically a "fake" cache: it won't ever store anything.

Yea, that's it! Head back to the browser and refresh. This is definitely not caching:
it takes an entire second because of the sleep(). Try again. Still not caching!
Now, change to the prod environment and refresh. Beautiful, this is still really,
really fast. Well that was easy.

Clearing prod Cache

Ok, this did work, but there's a small gotcha we're not considering. In config.yml,
change that thousands_separator back to a comma:

79 lines app/config/config.yml

... lines 1 - 35

# Twig Configuration

twig:

... lines 38 - 39

number_format:

thousands_separator:','

... lines 42 - 79

Try this first in the dev environment: Yep! No problems. Now refresh
in the prod environment. Huh, it's still a period. What gives?

Here's the thing: the prod environment is primed for speed. And that means, when
you change any configuration, you need to manually clear the cache before you'll
see those changes.

How do you do that? Simple: in the terminal, run:

./bin/console cache:clear --env=prod

You see, even the console script executes your app in a specific environment. By
default it uses the dev environment. Normally, you don't really care. But for
a few commands, you'll want to switch using this flag.

Ok, back to the browser. Refresh in prod. Boom! There's our comma!

The other Files: services.yml, security.yml, etc

Alright, what about all of these other configuration files. It turns out, there
all part of the exact same configuration system we've just mastered. So where is
parameters.yml loaded? Well, at the top of config.yml its imported, along with
security.yml and services.yml:

79 lines app/config/config.yml

imports:

-{resource:parameters.yml}

-{resource:security.yml}

-{resource:services.yml}

... lines 5 - 79

The key point is that all of the files are just loading each other: it's all the
same system.

In fact, I could copy all of security.yml, paste it into config.yml, completely
delete security.yml and everything would be fine. In fact, the only reason
security.yml even exists is because it feels good to keep that stuff in its
own file. The same goes for services.yml - a big topic we'll talk about in the
future.

Now, parameters.ymlis a little special. Let's find out why.

Leave a comment!

We are happy to hear from you :)I believe you just need to clear cache for your prod environment. Prod environment caches a lot of things and you have to manually specify whenever you want to clear it ($ php bin/console cache:clear -e prod)

If that's not the case, let us know! Have a nice day

2018-01-28Chris R

Hi, Team knpuniversity.com. You may hear more from me because I'm working through the Symfony3 courses with the ultimate goal of starting work on a project in Symfony4, so I'll be around here for a while....

When I get about a minute into this video, the step to refresh the prod page (http://localhost:8000/app.php/genus/octopus) results in a 500 error. The dev side works fine. Here's the error from the server log:

[2018-01-29 00:42:18] request.CRITICAL: Uncaught PHP Exception Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: "You have requested a non-existent service "doctrine_cache.providers.my_markdown_cache"." at D:\Users\c\src\ctr-symfony3-fundamentals\var\bootstrap.php.cache line 2129 {"exception":"[object] (Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException(code: 0): You have requested a non-existent service \"doctrine_cache.providers.my_markdown_cache\". at D:\\Users\\c\\src\\ctr-symfony3-fundamentals\\var\\bootstrap.php.cache:2129)"} []

Can you help me figure out what's causing this? I've tried duplicating the code for the caching in config.yml to config_prod.yml as a last resort to make sure the service wasn't missing, but that didn't help so I removed it. I'm using the composer.json from the course download, so Symfony 3.1.4 on PHP 7.2.1, and I'm using the server:run command to execute the code.

Thank you!

2017-08-02Jeroen Noten

allright, thanks!

2017-08-02Victor Bocharsky

Hey Jeroen,

You get this warning because cache:clear command will only clear the cache in Symfony 4.0. For now, this command clear the cache and then warm it up. So the correct way for Symfony 3.3 is:

./bin/console cache:clear --no-warmup./bin/console cache:warmup

and you won't get this warning anymore.

But since Symfony 4.0 you can just do:

./bin/console cache:clear./bin/console cache:warmup

Because cache:clear won't warm up the cache anymore.

Cheers!

2017-08-01Jeroen Noten

Why do I get this warning when clearing the cache?

[WARNING] Calling cache:clear without the --no-warmup option is deprecated since version 3.3. Cache warmup should be done with the cache:warmup command instead.

2017-07-11Josk

I've just noticed that by accident I started this course after the first videos. Now, I started from the beginning and it's everything ok, thank you.

2017-07-10Victor Bocharsky

Hey Josh,

Have you properly installed this bundle, i.e. download it with Composer and register this bundle in AppKernel? Please, double check it. We did all the installation and configuration of it here: https://knpuniversity.com/s... . Also, try to clear the cache.

Cheers!

2017-07-07Josk

Hi, in my config.yml there is no doctrine cache section. It stops at spool: {type: memory}If I write it in that place it shows me:

There is no extension able to load the configuration for "doctrine_cache" (in C:\aqua_note\app/config\config.yml). Looked for namespace "doctrine_cache", found "framework", "security", "twig", "monolog", "swiftmailer", "doctrine", "sensio_framework_extra", "debug", "web_profiler", "sensio_distribution".

Ah, great question! The point of this configuration is that this will create a "cache" service for us - specifically this creates doctrine_cache.providers.my_markdown_cache. We're then referencing and using this service in our code. So, one way or another, we DO need this service to always exist... which means that in dev environment, we need this service to exist... but not actually to do any real caching. By setting the type to array it effectively means "create a cache object, but of a type that doesn't actually cache". But I totally hear you - the bundle (DoctrineCacheBundle) *could* have setup their configuration so that, instead of a type: array, you could set some enabled: false flag, and behind the scenes, this would switch you to the "array" type automatically (or to the "void" type - which *truly* caches nothing - the array type at least "caches" within a process, so that if you ask for a value 2 times, it only calculates it once). It's just a matter of taste - I like the enabled: false idea, but also, since that would change the type behind-the-scenes, the type: array or type: void might be clearer to some people!

Cheers!

2017-03-08Richie Hamburg

Setting the doctrine_cache type to array to "stop logging" is pretty ugly. Why would there not be an "enabled" flag?