简体   繁体   English

Symfony2-将所有路由加载到控制器中(不重建缓存)

[英]Symfony2 - Load all routes in controller (without rebuilding cache)

I'm trying to create a Symfony form with a ChoiceType field that contains all the routes currently available. 我试图用一个ChoiceType字段创建一个Symfony表单,其中包含当前可用的所有路由。 This form has the purpose of adding these routes to a module in our backend. 此表单的目的是将这些路由添加到我们后端的模块中。 I've already created a Module and Route entity. 我已经创建了一个ModuleRoute实体。 These are used to render the correct navigation structure for the users working on a specific module. 这些用于为在特定模块上工作的用户呈现正确的导航结构。

I've used Router#getRouteCollection() inside the form controller, using this the routing cache will be rebuild on every request to the page. 我已经在表单控制器中使用了Router#getRouteCollection() ,使用该路由, 将在对页面的每个请求上重建路由缓存

I was thinking about a command that uses this method and adds all the routes to the database. 我正在考虑使用此方法并将所有路由添加到数据库的命令。 However this means that every time a route is added, this command needs to be executed as well. 但是,这意味着每次添加路由时,也需要执行此命令。 This seems like a step too much for such a simple task. 对于如此简单的任务,这似乎太过分了。

What would be the best practice to load these routes inside the controller? 在控制器内部加载这些路由的最佳实践是什么? And would it be possible to do this without: 并且可以做到这一点而无需:

  • Rebuilding the cache every request when using getRouteCollection() 使用getRouteCollection()时重建每个请求的缓存
  • Executing an extra command every time a route has been added to the Collection 每次将路线添加到集合中时执行一条额外的命令
  • Creating a controller that uses getRouteCollection() and save the results in the session, cache etc. 创建一个使用getRouteCollection()的控制器,并将结果保存在会话,缓存等中。

Thanks for thinking with me. 感谢您与我一起思考。

Kind regards 亲切的问候

I am not exactly sure for what use-case you need this routes but another problem will be that there are routes that you probably dont want to have in your routes list because they are for example routes from the profiler or AJAX routes. 我不确定要使用哪种路由,但是另一个问题是路由列表中可能不想包含某些路由,因为它们例如是探查器路由或AJAX路由。 So a better approach could be to maintain your own list of routes in a database table where the user can make his choice from. 因此,更好的方法可能是在数据库表中维护自己的路由列表,用户可以从中进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM