Looking up Routes from a Database: Symfony CMF DynamicRouterEdit this page
Warning: You are browsing the documentation for Symfony 4.2, which is no longer maintained.
Read the updated version of this page for Symfony 6.1 (the current stable version).
The core Symfony Routing System is excellent at handling complex sets of routes. A highly optimized routing cache is dumped during deployments.
However, when working with large amounts of data that each need a nice readable URL (e.g. for search engine optimization purposes), the routing can get slowed down. Additionally, if routes need to be edited by users, the route cache would need to be rebuilt frequently.
For these cases, the
DynamicRouter offers an alternative approach:
- Routes are stored in a database;
- There is a database index on the path field, the lookup scales to huge numbers of different routes;
- Writes only affect the index of the database, which is very efficient.
When all routes are known during deploy time and the number is not too
high, using a custom route loader is the
preferred way to add more routes. When working with just one type of
objects, a slug parameter on the object and the
annotation work fine (see FrameworkExtraBundle) .
DynamicRouter is useful when you need
Route objects with
the full feature set of Symfony. Each route can define a specific
controller so you can decouple the URL structure from your application
The DynamicRouter comes with built-in support for Doctrine ORM and Doctrine
PHPCR-ODM but offers the
ContentRepositoryInterface to write a custom
loader, e.g. for another database type or a REST API or anything else.
The DynamicRouter is explained in the Symfony CMF documentation.