Php Symofny2获取可用逻辑控制器名称的列表
我需要显示一个选项,其中包含所有可用控制器的列表,作为逻辑控制器名称Php Symofny2获取可用逻辑控制器名称的列表,php,symfony,routing,Php,Symfony,Routing,我需要显示一个选项,其中包含所有可用控制器的列表,作为逻辑控制器名称AcmeBundle:ControllerName:ActionName 我看到CLI命令php-app/console-router:debug转储了一个类似的列表,但带有控制器名称,例如fos\u-user\u-security\u-login 如何向Symfony询问其逻辑控制器名称表示 谢谢 正如@hous所说,这是有用的,但不完整,其公认的答案具有误导性 A)获取控制器 通过这段代码,我得到了所有的控制器,但是使用了它
AcmeBundle:ControllerName:ActionName
我看到CLI命令php-app/console-router:debug
转储了一个类似的列表,但带有控制器名称,例如fos\u-user\u-security\u-login
如何向Symfony询问其逻辑控制器名称表示
谢谢 正如@hous所说,这是有用的,但不完整,其公认的答案具有误导性
A)获取控制器
通过这段代码,我得到了所有的控制器,但是使用了它们的FQCN::method
或服务: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
符号是::build()的生成方法的正确参数。服务表示法未被解析,因为它由::createController()中的以下代码处理`
B)生成逻辑控制器名称
因此,我所要做的就是过滤掉我不想要的控制器,{FOS;framework;etc},并为每个选定的控制器提供build()
。例如,在我的例子中,只选择与我的Bundle名称空间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;
}
}
这对你有帮助吗[
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;
}
}