symfony-cmf/routing-bundle
Symfony CMF Routing Bundle adds advanced, dynamic routing to Symfony using a content repository. Supports route documents, redirects, nested routes, locale-aware patterns, and custom route providers to manage URLs from CMS content or databases.
Installation
Add the bundle to your composer.json:
composer require symfony-cmf/routing-bundle
Register it in config/bundles.php:
return [
// ...
SymfonyCmf\Bundle\RoutingBundle\RoutingBundle::class => ['all' => true],
];
Basic Configuration
Configure the bundle in config/packages/symfony_cmf_routing.yaml:
symfony_cmf_routing:
chain_router:
routers_by_id:
router.default: ~
router.dynamic: symfony_cmf_routing.dynamic_router
dynamic_router:
persistence:
phpcr: ~ # or 'doctrine_orm', 'doctrine_mongodb', etc.
First Use Case: Dynamic Routes from Database
Route with path, controller, priority fields).@Route (or use YAML/XML) and configure the dynamic router to fetch routes from your ORM/ODM.use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class Route
{
#[ORM\Id, ORM\Column(type: 'integer')]
private ?int $id;
#[ORM\Column(type: 'string')]
private string $path;
#[ORM\Column(type: 'string')]
private string $controller;
#[ORM\Column(type: 'integer')]
private int $priority = 0;
}
config/packages/symfony_cmf_routing.yaml:
dynamic_router:
persistence:
orm:
route_repository: 'App\Repository\RouteRepository'
routers_by_id (e.g., router.default for Symfony’s default router, router.dynamic for DB routes).chain_router:
routers_by_id:
router.dynamic: symfony_cmf_routing.dynamic_router # High priority
router.default: ~ # Fallback
SymfonyCmf\Routing\RouteProviderInterface to fetch routes from your data source.
use SymfonyCmf\Routing\RouteProviderInterface;
class DoctrineRouteProvider implements RouteProviderInterface
{
public function getRoutes(): array
{
return $this->routeRepository->findAll()->toArray();
}
}
dynamic_router:
persistence:
orm:
route_provider: 'App\Provider\DoctrineRouteProvider'
dynamic_router:
cache:
enabled: true
lifetime: 3600 # Cache for 1 hour
priority field (or custom logic) to determine route matching order.priority values are matched first.Route entity, set a default or dynamic priority:
#[ORM\Column(type: 'integer')]
private int $priority = 100; // Higher = matched first
chain_router:
routers_by_id:
router.dynamic: symfony_cmf_routing.dynamic_router # DB routes (high priority)
router.yaml: symfony_cmf_routing.yaml_router # Static routes (low priority)
SymfonyCmf\Routing\RouteObjectInterface for custom route logic.use SymfonyCmf\Routing\RouteObjectInterface;
class CustomRoute implements RouteObjectInterface
{
private string $path;
private string $controller;
private array $requirements;
public function getPath(): string { return $this->path; }
public function getController(): string { return $this->controller; }
public function getRequirements(): array { return $this->requirements; }
}
dynamic_router:
persistence:
custom:
route_class: 'App\Routing\CustomRoute'
provider: 'App\Provider\CustomRouteProvider'
routers_by_id order is correct (higher priority first).$router = $container->get('router');
$routes = $router->getRouteCollection()->getResources();
dump($routes);
priority field in your route entity.php bin/console debug:router
php bin/console cache:clear
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use SymfonyCmf\Routing\Event\RouteUpdateEvent;
class RouteCacheSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
RouteUpdateEvent::NAME => 'onRouteUpdate',
];
}
public function onRouteUpdate(RouteUpdateEvent $event)
{
$this->cacheClearer->clear('cmf_routing');
}
}
lifetime in config (e.g., 3600 seconds).SELECT with ORDER BY priority DESC).RouteObjectInterface causes errors.RouteObjectInterface or use a mapper:
use SymfonyCmf\Routing\RouteObjectMapperInterface;
class DoctrineRouteMapper implements RouteObjectMapperInterface
{
public function createRouteObject(array $data): RouteObjectInterface
{
return new CustomRoute(
$data['path'],
$data['controller'],
$data['requirements'] ?? []
);
}
}
config/packages/symfony_cmf_routing.yaml:
dynamic_router:
persistence:
orm:
route_mapper: 'App\Mapper\DoctrineRouteMapper'
composer.json for Symfony 6 compatibility).Router service to work with Symfony 6’s RouterInterface.RouteProviderInterface for non-DB sources (e.g., API, flat files).SymfonyCmf\Routing\RouteMatcherInterface forHow can I help you explore Laravel packages today?