来自数据库的 Symfony2 路由规则

问题描述:

如果我想从数据库添加路由规则,我该怎么办.我应该使用 https://github.com/symfony-cmf/ChainRoutingBundle 还是其他更简单的解决方案?

What should I do, if I want add routing rules from database. Should I use https://github.com/symfony-cmf/ChainRoutingBundle or there is another simpler solution?

kernel.request 并使用 $request->getPathInfo() 查询路线数据库.如果找到,请在请求上设置 _controller 属性.

Listen to kernel.request and use the $request->getPathInfo() to query the database for a route. If you find one, set the _controller attribute on the request.

$request->attributes->set('_controller', $row['controller']);

如果您正在运行 master 分支(Symfony 2.1),您的监听器的优先级将需要大于 32,以便在路由器之前运行.

If you are running the master branch (Symfony 2.1) your listener will need to have a priority greater than 32 so it will run before the router.