【问题标题】:Symofny2 Get a listing of available Logical Controller NamesSymfony2 获取可用逻辑控制器名称的列表
【发布时间】:2014-07-14 12:24:04
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

标签: php symfony routing


【解决方案1】:

正如@hous 所说,this post 很有用,但不完整,并且其接受的答案具有误导性。

A) 获取控制器

通过此代码,我得到了所有控制器,但使用了 FQCN::methodservice: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;
}
}

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2010-12-30
    • 2010-10-21
    相关资源
    最近更新 更多