Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.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 Symfony2传递给构造函数条令实体管理器引发错误_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php Symfony2传递给构造函数条令实体管理器引发错误

Php Symfony2传递给构造函数条令实体管理器引发错误,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,在我的每个控制器上,我需要从构造函数运行安全强制函数。问题是,我无法在构造函数中运行条令实体管理器来从会话加载用户id,并从用户实体上调用findby函数。 我发现的唯一方法是对我的每个操作调用这个安全强制函数,这不是最优的——代码不干净,还有很多函数调用。 我搜索并测试了stackoverflow中的许多用户解决方案,其中许多是其他不工作代码的复制粘贴。 My services.yml文件: services: app.default_controller: class

在我的每个控制器上,我需要从构造函数运行安全强制函数。问题是,我无法在构造函数中运行条令实体管理器来从会话加载用户id,并从用户实体上调用findby函数。 我发现的唯一方法是对我的每个操作调用这个安全强制函数,这不是最优的——代码不干净,还有很多函数调用。 我搜索并测试了stackoverflow中的许多用户解决方案,其中许多是其他不工作代码的复制粘贴。 My services.yml文件:

services:
    app.default_controller:
        class: AppBundle\Controller\DefaultController
        arguments:
            - @doctrine.orm.entity_manager
我的DefaultController类(其示例控制器):


请允许我建议一种不同的方法。您最好使用symfony事件系统:)

您可以设置要在控制器的任何操作之前和之后执行的侦听器

只需标记您想要订阅的事件,并将其发送到您为安全目的创建的服务

签出与此相关的symfony文档

祝你好运!:)

<?php

namespace AppBundle\Controller;
use Doctrine\ORM\EntityManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    protected $em;
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', array(
            'base_dir' => realpath($this->getParameter('kernel.root_dir').'/..'),
        ));
    }

    public function __construct(EntityManager $em = null){
        $this->em=$em;

      $personRepository = $this->getDoctrine()->getManager()->getRepository('AppBundle\Entity\Person');
        //$em = $this->getDoctrine()->getManager();

    }
}