Symfony2 dynamic routing - caching issue
This is quite inefficient, because with each new route you have to clear cache, so you'll be bound by hdd/ssd with useless clear cache.The alternative is to create a new method in controller which accepts a dynamic page on GET and to show the dynamic content in twig.
You can create a service to render the dynamic pages, which will simplify things.
Do you have looked at the DynamicRouter
from the symfony-cmf
project? I think this fits your needs and is exactly created for your use case.
You current implementation has some really issues you should know about. First of all, you have to clear the routing cache, for each route you create/edit/delete. This leads to race conditions and memory peaks for no reason.
The default implementation from symfony is to handel static routes, not dynamic ones.
I researched and tried out a bit and I found out that you can just delete the following files:
for dev:
/app/cache/dev/appDevUrlGenerator.php/app/cache/dev/appDevUrlGenerator.php.meta/app/cache/dev/appDevUrlMatcher.php/app/cache/dev/appDevUrlMatcher.php.meta
for prod:
/app/cache/prod/appProdUrlGenerator.php/app/cache/prod/appProdUrlMatcher.php
There is only one minior downside of this. I am using the current route to determine if a menu item is active or not:
{% set currentPath = path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) %}...<li{% if currentPath == path('mybundle_default_index') %} class="active"{% endif %}>
In this case app.request.attributes.get('_route')
is still cached as a route that might not exist anymore. I don't know yet if this only concerns the twig cache or other parts too.
Also I don't understand why you would have to delete the whole cache on each page load? You only have to clear the cache when new routes are added.