我需要显示所有可用控制器列表的选项作为逻辑控制器名称AcmeBundle:ControllerName:ActionName
我看到CLI命令php app/console router:debug
转储了类似的列表,但是带有控制器名称,例如fos_user_security_login
。
如何向Symfony询问其逻辑控制器名称表示?
谢谢!
答案 0 :(得分:0)
正如@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属性。
这是构建docBlock
/**
* 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;
}
}