Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 2.8服务问题_Php_Symfony 2.8 - Fatal编程技术网

Php Symfony 2.8服务问题

Php Symfony 2.8服务问题,php,symfony-2.8,Php,Symfony 2.8,在过去的4个小时里,我一直在努力理解Symfony 2服务的逻辑以及它们如何集成到应用程序中 基本上,我试图通过服务设置EntityManager,并在控制器中使用它 我有以下结构 Bundle1/Controller/Bundle1Controller.php Bundle1/Services/EntityService.php Bundle2/Controller/Bundle2Controller.php Bundle3/Controller/Bundle3Controller.php .

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

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

我有以下结构

Bundle1/Controller/Bundle1Controller.php
Bundle1/Services/EntityService.php
Bundle2/Controller/Bundle2Controller.php
Bundle3/Controller/Bundle3Controller.php
....
我正在尝试使用不同的入口点创建RESTAPI,这就是为什么我使用多个bundle
bundle2、bundle3….

逻辑如下:

  • 向Bundle2/Controller/Bundle2Controller.php发送POST
  • Bundle2Controller.php实例一个新的()Bundle1Controller.php
  • 在Bundle1控制器中,我想访问一个服务
    实体\u服务
    ,以获取我的EntityManager
我有两个案例,我设法着陆

  • Bundle1/Controller/Bundle1 Controller
    中,如果我尝试
    $this->container
    $this->get('entity\u service')
    每次我都会得到一个
    null
  • 如果我在
    Bundle2/Controller/Bundle2Controller
    中设置容器并尝试
    $this->get('entity\u service')
    I get
    您请求了一个不存在的服务“entity\u service”
  • 我将把所有代码放在下面

    Bundle1/Controller/Bundle1控制器

    <?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;
    
        }
    }
    
    有人能帮我解决这个问题吗? 我如何注册一个服务并从任何地方调用它,无论是捆绑包还是其他服务

  • 您应该检查services.yml的位置,以及它是否已导入config.yml中
  • 您不能只是实例化一个控制器并期望它工作,您需要设置容器
  • 但是您可以使用调用EntityManager而不需要任何其他服务
    $this->get('doctrine.orm.entity_manager')

    我无法理解你的结构或者你想要实现什么,但是如果你想保持这种结构,这些是你可以选择的

    <?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']);
    
        }
    }
    
    <?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:    
    entity_service:
            class: Bundle1\Services\EntityService
            arguments: [ "@doctrine.orm.entity_manager" , "@service_container"   ]