在特定 Bundle 中查找路由
我正在使用 symfony 2 实现一个 Web 应用程序。它有一个通过“CoreBundle”提供服务的主页,该主页加载 div 中其他应用程序包的内容。为了处理这个问题,我想让 CoreBundle 路由捕获所有路由,然后将请求转发到应用程序包,以便获取 div 中的内容。
路线是:
@Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"})
这很好用。它捕获 $app_name 设置为应用程序名称(第一个子目录)的所有路由,以及 $garbage 内的其余 uri。
我的问题是尝试在特定应用程序中找到 $garbage 的路线。核心包知道应用程序的包名称。
到目前为止,我已经尝试了以下两件事:
使用路由器服务查找路由,但尝试删除捕获所有路由:
$router->getRouteCollection()->remove("_core_app_garbage");
这似乎没有改变任何东西(我返回的路由仍然是包罗万象的)。
创建我自己的路由器。我尝试了各种配置,但似乎需要大量的类创建和特定的类创建(在我的例子中是 AnnotationDirectoryLoader )。这对于我习惯使用 Symfony 来说似乎过于困难,而且如果我决定更改路由格式(例如更改为 YAML),它就不可移植。
有没有一种快速且简单的方法可以做到这一点?
I am implementing a web app using symfony 2. It has a main page that is served through a "CoreBundle" which loads up the contents of other app bundles within a div. To handle this, I want to have the CoreBundle route catch all routes and then forward the request to the app bundles in order to get the content within the div.
the route is:
@Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"})
This works great. It captures all routes with $app_name set to the name of the app (the first subdirectory), and the rest of the uri inside $garbage.
My problem is trying to find the route for $garbage within the specific app. The core bundle has knowledge of the bundle name for the app.
So far I have tried the following two things:
Use the router service to find the route but trying to remove the catch all route:
$router->getRouteCollection()->remove("_core_app_garbage");
That doesn't seem to change anything (the route I get back is still the catch all).
Creating my own router. I tried various configurations but it seems to take a lot of class creations and specific class creations ( AnnotationDirectoryLoader in my case ). That seems overly difficult to what I am used to with Symfony and it is not portable if I decide to change the routing format (to YAML for example).
Is there a quick and easy way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个非常好的解决方案,尽管它并不完美:
基本上我使用“routing.loader”服务加载特定包的路由集合。然后我创建自己的 UrlMatcher 来匹配这些路由。
这里的问题是我已经硬编码为仅在捆绑包内的“Controller”文件夹中查找控制器。
I found a pretty good solution, although it isn't perfect:
Basically I load the route collection for a specific bundle using the 'routing.loader' service. Then I create my own UrlMatcher to match against those routes.
The problem here is that I have hard coded to only look for controllers in the "Controller" folder within the bundle.