Symofny2 获取可用逻辑控制器名称的列表 [英] Symofny2 Get a listing of available Logical Controller Names

查看:29
本文介绍了Symofny2 获取可用逻辑控制器名称的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示一个选项,其中包含所有可用控制器的列表作为逻辑控制器名称 AcmeBundle:ControllerName:ActionName

I need to show a choice with a list of all available controllers as Logical Controller Names AcmeBundle:ControllerName:ActionName

我看到 CLI 命令 php app/console router:debug 转储了一个类似的列表,但带有控制器名称,例如fos_user_security_login.

I see that CLI command php app/console router:debug dumps a similar listing, but with controller names, e.g. fos_user_security_login.

我如何向 Symfony 询问他们的逻辑控制器名称表示?

How can I ask Symfony for their Logical Controller Name representation?

谢谢!

推荐答案

正如@hous 所说,这篇文章 很有用,但不完整,其公认的答案具有误导性.

As @hous said, this post was useful, but incomplete and its accepted answer misleading.

A) 获取控制器

通过此代码,我获得了所有控制器,但使用了它们的 FQCN::methodservice:method 符号.

With this code I get all controllers, but with their FQCN::method or service:method notation.

// in a Controller.
$this->container->get('router')->getRouteCollection()->all()

一些背景

前面的方法将返回一个很大的路由数组.跟随一键 => 值:

The previous method will return a big array of routes. Follows one key => value:

'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()`

The FQCN::method notation is the right argument to the build method of ControllerNameParser::build(). The service notation is not parsed, as it gets handled by the following code in 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;构架;etc} 并为 build() 提供每个选定的内容.例如.通过仅选择与我的包命名空间 Application\*Bundle 匹配的 _controller 属性.

So all I have to do is filter out the controllers I don't want, {FOS; framework's; etc} and feed build() with each selected one. E.g. by selecting only the _controller attributes that matches my bundles namespace Application\*Bundle in my case.

这是构建文档块

/**
 * 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;
}
}

这篇关于Symofny2 获取可用逻辑控制器名称的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆