I'm trying to create dynamic routes as I have created a CMS where each page created can be associated to a route. I'm using the example from this link - http://php-and-symfony.matthiasnoback.nl/2012/01/symfony2-dynamically-add-routes/ and all works fine, however the routing is cached, therefore one route will work but then the next won't unless I clear the cache. Is it possible to remove just the routing cache at this stage or is there another alternative? I don't want to remove the whole cache directory on each page load as that wouldn't make sense. Here is the example code:
namespace Acme\RoutingBundle\Routing;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ExtraLoader implements LoaderInterface
{
private $loaded = false;
public function load($resource, $type = null)
{
if (true === $this->loaded) {
throw new \RuntimeException('Do not add this loader twice');
}
$routes = new RouteCollection();
$pattern = '/extra';
$defaults = array(
'_controller' => 'AcmeRoutingBundle:Demo:extraRoute',
);
$route = new Route($pattern, $defaults);
$routes->add('extraRoute', $route);
return $routes;
}
public function supports($resource, $type = null)
{
return 'extra' === $type;
}
public function getResolver()
{
}
public function setResolver(LoaderResolver $resolver)
{
// irrelevant to us, since we don't need a resolver
}
}
Then I've made a service for the ExtraLoader:
<!-- in /src/Acme/RoutingBundle/Resources/config/services.xml -->
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="acme.routing_loader" class="Acme\RoutingBundle\Routing\ExtraLoader">
<tag name="routing.loader"></tag>
</service>
</services>
</container>
The last thing we need, is a few extra lines in /app/config/routing.yml:
AcmeRoutingBundle:
resource: .
type: extra
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.
I've resolved this problem in my own CMS.
At first I overrode the base
Router
class:and extended it:
Secondly I created a service
CacheService
:and added the following method:
So now I can call this method when I need to clear cache for current or specific environment.
I researched and tried out a bit and I found out that you can just delete the following files:
for dev:
for prod:
There is only one minior downside of this. I am using the current route to determine if a menu item is active or not:
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.
Do you have looked at the
DynamicRouter
from thesymfony-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.