Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.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
Model view controller 关于Symfony';s控制器_Model View Controller_Symfony - Fatal编程技术网

Model view controller 关于Symfony';s控制器

Model view controller 关于Symfony';s控制器,model-view-controller,symfony,Model View Controller,Symfony,看看那个代码 <?php namespace Sestante\SestanteBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Sestante\SestanteBundle\Model\StrutturaManager; class MainController extends

看看那个代码

<?php

namespace Sestante\SestanteBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sestante\SestanteBundle\Model\StrutturaManager;

class MainController extends Controller
{
    public function indexAction(Request $request)
    {
        return $this->render('SestanteSestanteBundle:Main:index.html.twig');
    }

    public function showLodgingsAction(Request $request)
    {
        $repo = $this->getDoctrine()->getRepository('SestanteSestanteBundle:Struttura');
        $usr = $this->get('security.context')->getToken()->getUser();
        $usrId = $usr->getId();
        $sm = new StrutturaManager($repo);
        $lodgingList = $sm->retrieveLodgingsFromUser($usrId);
        return $this->render('SestanteSestanteBundle:Main:showLodgings.html.twig',array('lodgingList' => $lodgingList));
    }
}

StrutturaManager
定义为,并将
EntityManager
注入其中。这样,经理就可以访问您需要的存储库,而管理员就不会了解条令或存储库,这是一种很好的做法。

好的,这是我的第一个想法。然而,这里有一个“但是”。假设我已经听从了你的建议。我有我的服务等等。我必须在每次操作中使用
$this->get('struttura\u manager')
来回忆它(假设struttura\u manager是此服务的注册名称)?是的。或者你可以通过构造函数注入管理器和其他服务,然后像
$this->strutturamager
那样使用它们。这对我来说是有意义的,但让我担心的是另一个-最后一点,我保证:P-是我有,比方说,十个实体,每个实体我都有一个服务,比如xxmanager,yyyyManager等等?是的,模特和他们的经理之间几乎总是
1:1