dongmao3148
2017-05-24 13:18
浏览 47
已采纳

如何在Symfony中以编程方式获取服务列表?

I want to have a list of services and class names in my web application. I can use this command in console:

php bin/console debug:container

And I get something like this:

Symfony Container Public Services
=================================

 -------------------------------------------------------------------- --------------------------------------------------------------------------------------------
  Service ID                                                           Class name
 -------------------------------------------------------------------- --------------------------------------------------------------------------------------------
  annotation_reader                                                    Doctrine\Common\Annotations\CachedReader
  app.annotations.softdelete.driver                                    AppBundle\Doctrine\SoftDelete\Mapping\Driver\Annotation
  app.annotations.translate.driver                                     AppBundle\Doctrine\Mapping\Driver\TranslateDriver
  app.be_auth_controller.listener                                      AppBundle\EventListener\BeAuthControllerListener

I want to have this information on a web page using Symfony 3.

I created a service and I used:

$this->container->getServiceIds();

which returns something like:

[
  0 => "service_container"
  1 => "annotation_reader"
  2 => "annotations.reader"
  3 => "app.annotations.softdelete.driver"
  4 => "app.annotations.translate.driver"
...
]

I don't know, how to get the class names.

In any cases works this:

get_class($this->container->get($this->container->getServiceIds()[1]))

But in some other cases it throws different exceptions.

Do you have any idea?

Thanks.

图片转代码服务由CSDN问答提供 功能建议

我想在我的Web应用程序中有一个服务和类名列表。 我可以在控制台中使用此命令:

  php bin / console debug:container 
   
 
 

我得到的东西 像这样:

  Symfony Container Public Services 
 =============================  ====== 
 
 ----------------------------------------  ---------------------------- ----------------------  --------------------------------------------------  -------------------- 
服务ID类名
 ----------------------  ---------------------------------------------- ----  --------------------------------------------------  -------------------------------------- 
 annotation_reader Doctrine \ Common \ Annotations \ CachedReader 
  app.annotations.softdelete.driver AppBundle \ Doctrine \ SoftDelete \ Mapping \ Driver \ Annotation 
 app.annotations.translate.driver AppBundle \ Doctrine \ Mapping \ Driver \ TranslateDriver \  n app.be_auth_controller.listener AppBundle \ EventListener \ BeAuthControllerListener 
   
 
 

我想在使用Symfony 3的网页上获取此信息。

我创建了一项服务并使用了:

  $ this-> container-> getServiceIds(); 
   
  
 

返回类似于:

  [
 0 =>  “service_container”
 1 =>  “annotation_reader”
 2 =>  “annotations.reader”
 3 =>  “app.annotations.softdelete.driver”
 4 =>  “app.annotations.translate.driver”
 ... 
] 
   
 
 

我不知道,如何获取类名。

在任何情况下都可以这样做:

  get_class($ this-> container-> get($ this-> container->  getServiceIds()[1]))
   
 
 

但在其他一些情况下它会引发不同的异常。

你有没有 想法?

谢谢。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • drus40229 2017-05-24 17:36
    最佳回答

    For get full definition of given service you can use ContainerBuilder and Symfony cache file.

    first create instance of ContainerBuilder:

    $container = new ContainerBuilder();
    

    then load cache file:

    $cachedFile = $this->container->getParameter('debug.container.dump');
    $loader = new XmlFileLoader($container, new FileLocator());
    $loader->load($cachedFile);
    

    now you can get full definition of your service like this:

    $definition = $container->getDefinition('service_name')
    $definition->getClass();
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题