代码之家  ›  专栏  ›  技术社区  ›  Trevor Donahue

Symfony 2.4.2-在不同捆绑包中的控制器之间传递实体管理器

  •  1
  • Trevor Donahue  · 技术社区  · 12 年前

    我在两层控制器之间传递实体管理器时遇到了问题。

    我正在构建的系统具有以下结构:

    2捆:

    核心捆绑包 (我们称之为后端控制器)

    这是包含所有模型(实体)和业务规则/逻辑的捆绑包。

    API捆绑包 (称之为前端控制器)

    负责检查传入的api密钥的权限,并与Core捆绑包通信以获取信息。

    以下是用户控制器和实体的示例:

    用户控制器.php 在APIBundle中:

    <?php
    
    namespace Acme\Bundle\APIBundle\Controller;
    
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpFoundation\Request;
    
    use Acme\Bundle\CoreBundle\Controller\UserController as User;
    
    
    use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
    
    class UserController extends BaseController implements AuthenticatedController
    {
    
        public function readAction(Request $request) {
    
            $user = new User($this->getDoctrine()->getManager());
    
            $user->load(2);
    
            return $this->response($user);
        }
    }
    

    用户控制器.php 在CoreBundle中:

    <?php
    
    namespace Acme\Bundle\CoreBundle\Controller;
    
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
    use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
    use Symfony\Component\HttpFoundation\Response;
    
    use Acme\Bundle\CoreBundle\Entity\User;
    
    class UserController extends BaseController
    {
    
        function __construct($em) {
            parent::__construct($em);
            $this->entity = new User();
        }
    
        /**
         * Get userId
         *
         * @return integer 
         */
        public function getUserId()
        {
            return $this->entity->userId;
        }
    
        /**
         * Set firstName
         *
         * @param string $firstName
         * @return User
         */
        public function setFirstName($firstName)
        {
            $this->entity->firstName = $firstName;
    
            return $this;
        }
    
        // ...
        public function load($id) {
    
        if (!$this->entity instanceof \Acme\Bundle\CoreBundle\Entity\BaseEntity) {
            throw new \Exception('invalid entity argument');
        }
    
        $this->entity = $this->em->getRepository('AcmeCoreBundle:User')->find($id);
        }
    }
    

    请告诉我我做得对不对。每次在控制器之间传递实体管理器似乎很奇怪。

    也许有更好的方法?

    捆绑分离的想法有意义吗?

    谢谢你,我非常感谢你的每一个想法。

    2 回复  |  直到 12 年前
        1
  •  1
  •   qeek    12 年前

    如果CoreBundle UserController从未通过HTTP访问,其方法也不会返回Symfony\Component\HttpFoundation\Response的实例,那么它就不是真正的控制器。

    您应该更好地将其定义为服务,如在CoreBundle\service\User中,并通过DI容器注入EntityManager。

    服务.yml

      corebundle.userservice:
        class:  Acme\CoreBundle\Service\User
        arguments: [@doctrine.orm.entity_manager]
    

    然后,它将从Acme\Bundle\APIBundle\Controller\UserController提供,包括以下内容:

    $user = $this->get('corebundle.userservice');
    

    当然,您也可以将Acme\Bundle\APIBundle\Controller\UserController定义为一个单独的服务,然后注入“corebundle.userservice”,以方便使用。

    我建议你阅读Symfony的文档 Dependency Injection .

        2
  •  -1
  •   manuk    12 年前

    在entity类中搜索获取实体管理器是错误的方法!

    在CoreBundle中,使用与Entity类相同的UserController.php。

    阅读 docs 了解如何正确使用symfony中的存储库。

    在APIBundle的UserController中,必须调用自定义存储库函数。此存储库在实体中声明。

    推荐文章