使用symfony的服务注入

  1. 在app/config/services.yml中添加
       tron.send_weixin_message:
           class: Tron\LolBundle\Common\SendWeiXinMessage //这一行代表要将service插入的位置
           arguments: ["@doctrine.orm.entity_manager"]    //这一行代表要插入的服务的名称,这里插的是一个现有的SYMFONY的doctrine相关的服务
    

    格式一定要正确,空隔不可以多也不可以少。

  2. 在相应的文件中要填加构造函数
      private $em;
    
      public function __construct(EntityManager $em)
      {
        $this->em = $em;
      }
    
  3. 在其它地方使用时要注意使用
    在controller中 
    $this->get('tron.send_weixin_message')
    而在command中要使用 
    $lolWeiXinInfo = $this->getContainer()->get('tron.send_weixin_message');
    
    来取代$lolWeiXinInfo = new SendWeiXinMessage();
    

     

posted @ 2015-12-01 15:31  S大好人S  阅读(196)  评论(0编辑  收藏  举报