Symfony 2.8服务问题

时间:2017-06-09 13:49:31

标签: php symfony-2.8

自从过去4个小时以来,我一直在努力了解Symfony 2服务的逻辑以及它们如何集成到应用程序中......

基本上我正在尝试通过服务设置我的EntityManager并在控制器中使用它

我有以下结构

Bundle1/Controller/Bundle1Controller.php
Bundle1/Services/EntityService.php
Bundle2/Controller/Bundle2Controller.php
Bundle3/Controller/Bundle3Controller.php
....

我正在尝试使用不同的入口点制作REST API,这就是我使用多个捆绑bundle2,bundle3....

的原因

逻辑如下:

  • 向Bundle2 / Controller / Bundle2Controller.php
  • 发出POST
  • Bundle2Controller.php实例一个new()Bundle1Controller.php
  • Inside Bundle1Controller我想访问服务entity_service以获取我的EntityManager

我有2个案例,我设法降落......

  1. Bundle1/Controller/Bundle1Controller如果我尝试$this->container$this->get('entity_service'),我每次都会null
  2. 如果我在Bundle2/Controller/Bundle2Controller中设置容器并尝试$this->get('entity_service'),我会You have requested a non-existent service "entity_service"
  3. 我将把所有代码放在

    之下

    Bundle1 /控制器/ Bundle1Controller

    <?php
    
    namespace Bundle1\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use EntityBundle\Entity\TestEntity;
    use Symfony\Component\DependencyInjection\ContainerAwareInterface;
    
    class Bundle1Controller extends Controller
    {
    
        /**
         * @param $response
         * @return array
         */
        public function verifyWebHookRespone($response){
    
            $em = $this->get('entity_service')->getEm();
    
            $array = json_decode($response);
    
            $mapping = $em->getRepository('EntityBundle:TestEntity')
                   ->findBy(["phone" => $array['usernumber']]);
    
            return $mapping;
    
        }
    }
    

    bundle2中/控制器/ Bundle2Controller.php

    <?php
    
    namespace Bundle2\Controller;
    
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Bundle1\Controller\Bundle1Controller;
    
    class Bundle2Controller extends Controller
    {
    
        public function webhookAction(Request $request)
        {
            $data = $request->request->get('messages');
    
            $model = new Bundle1Controller();
            $responseMessage = $model->verifyWebHookRespone($data);
    
            return new Response($responseMessage, Response::HTTP_CREATED, ['X-My-Header' => 'My Value']);
    
        }
    }
    

    Bundle1 /服务/ EntityService.php

    <?php
    
    namespace EntityBundle\Services;
    
    use Doctrine\ORM\EntityManager;
    use Symfony\Component\DependencyInjection\Container;
    
    class EntityService
    {
        protected $em;
        private $container;
    
    
        public function __construct(EntityManager $entityManager, Container $container)
        {
            $this->em = $entityManager;
            $this->container = $container;
        }
    
        /**
         * @return EntityManager
         */
    
        public function getEm()
        {
            return $this->em;
        }
    }
    

    services.yml

    services:    
    entity_service:
            class: Bundle1\Services\EntityService
            arguments: [ "@doctrine.orm.entity_manager" , "@service_container"   ]
    

    任何人都可以帮我解决这个问题吗? 如何在任何地方注册服务并从其中调用它,无论是捆绑服务还是其他服务?

1 个答案:

答案 0 :(得分:1)

  1. 您应该检查您的services.yml的位置以及是否在config.yml
  2. 中导入
  3. 您不能只是实例化一个控制器并希望它能够工作,您需要设置容器。
  4. 但您可以使用;
  5. 调用EntityManager而无需任何其他服务

    $this->get('doctrine.orm.entity_manager');

    我无法理解你的结构或你想要达到的目标,但如果你想保持这种结构,那么这些是可以选择的。

相关问题