Symfony2 - 一种获取捆绑包中所有控制器的方法? [英] Symfony2 - A way to get all controllers of a bundle?
问题描述
有没有办法获得一个包的所有控制器(和类)?包括所有父控制器?
Is there a way to get all controllers (and the class) of a bundle ? Including all parent contollers ?
谢谢
推荐答案
最现实的方法是获取路由的端点.
The most realistic way to this is is by getting the endpoints of the routes.
控制器没有文档所暗示的必需结构.动作方法不需要在名称中加上 Action
后缀.以这种方式记录的原因是因为很多人在他们的路由 yml 中使用 catch all 配置,需要后缀来猜测端点.
A controller doesn't have a required structure as the documentation implies. An action method does not need to be suffixed with Action
in the name. The reason it is documented this way is because a lot of people use the catch all configuration in their routing yml, the suffix is needed to guess the endpoints.
当你打印出路线时:
print_r($this->container->get('router')->getRouteCollection()->all());
您将看到针对每个路由使用的控制器方法.这些路由在技术上不属于"任何包,因为您可以将路由指向任何类的任何方法.
You will see the controller methods used against each route. These routes technically don't 'belong' to any bundle since you can point a route at any method of any class.
但是,您可以使用列表和一些字符串魔法来告诉基于命名空间的包.
However, you can use the list and a bit of string magic to tell the bundle based on the namespace.
请记住,像 web_profiler.controller.profiler:infoAction
这样的 _controller
意味着该方法属于通过依赖注入实例化的服务.
Bare in mind that a _controller
like web_profiler.controller.profiler:infoAction
means the method belongs to a service instantiated via dependency injection.
这篇关于Symfony2 - 一种获取捆绑包中所有控制器的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!