正如@hous 所说,this post 很有用,但不完整,并且其接受的答案具有误导性。
A) 获取控制器
通过此代码,我得到了所有控制器,但使用了 FQCN::method 或 service:method 表示法。
// in a Controller.
$this->container->get('router')->getRouteCollection()->all()
一些背景
前面的方法将返回一个大的路由数组。跟随一键 => 值:
'admin_chacra' => // route name
object(Symfony\Component\Routing\Route)[1313]
...
private 'defaults' =>
array (size=1)
'_controller' => string 'Application\ColonizacionBundle\Controller\ChacraController::indexAction' (length=71)
FQCN::method 表示法是ControllerNameParser::build() 的构建方法的正确参数。服务符号没有被解析,因为它由ControllerResolver::createController() 中的以下代码处理`
$count = substr_count($controller, ':');
if (2 == $count) {
// controller in the a:b:c notation then
/* @var $this->parser ControllerNameParser parse() is the oposite of build()*/
$controller = $this->parser->parse($controller);
} elseif (1 == $count) {
// controller in the service:method notation
list($service, $method) = explode(':', $controller, 2);
return array($this->container->get($service), $method);
} else {
throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
}
B) 生成逻辑控制器名称
所以我要做的就是过滤掉我不想要的控制器,{FOS;构架;等} 并为每个选定的内容提供build()。例如。通过仅选择与我的包命名空间 Application\*Bundle 匹配的 _controller 属性。
这是构建文档块
/**
* Converts a class::method notation to a short one (a:b:c).
*
* @param string $controller A string in the class::method notation
*
* @return string A short notation controller (a:b:c)
*
* @throws \InvalidArgumentException when the controller is not valid or cannot be found in any bundle
*/
我的实现
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
class ActivityRoleControllerType extends AbstractType
{
...
/**
* Controller choices
*
* @var array
*/
private static $controllers = array();
/**
* Controller Name Parser
*
* @var ControllerNameParser
*/
private $parser;
/**
* expects the service_container service
*/
public function __construct(ContainerInterface $container)
{
$this->parser = new ControllerNameParser($container->get('kernel'));
self::$controllers = $this->getControllerLogicalNames(
$container->get('router')->getRouteCollection()->all(), $this->parser
);
}
/**
* Creates Logical Controller Names for all controllers under \Application\*
* namespace.
*
* @param Route[] $routes The routes to iterate through.
* @param ControllerNameParser $parser The Controller Name parser.
*
* @return array the ChoiceType choices compatible array of Logical Controller Names.
*/
public function getControllerLogicalNames(array $routes, ControllerNameParser $parser)
{
if (! empty(self::$controllers)) {
return self::$controllers;
}
$controllers = array();
/* @var $route \Symfony\Component\Routing\Route */
foreach ($routes as $route) {
$controller = $route->getDefault('_controller')
if (0 === strpos($controller, 'Application\\')) {
try {
$logicalName = $parser->build($controller);
$controllers[$logicalName] = $logicalName;
} catch (\InvalidArgumentException $exc) {
// Do nothing, invalid names skiped
continue;
}
}
}
asort($controllers);
return $controllers;
}
}