0
我在Symfony非常新。我在symfony 2.3 LTS中開始了我的第一個項目。在這個時候,我創建了許多不同的控制器。我的問題涉及到一個正確的程序。我在我的控制器中使用了不同的服務,如'翻譯器','表格工廠','會話','路由器','實體管理器','模板化'。基地控制器/服務Depedendency注入
我在「service.yml」基本服務
myapp.controller.base:
class: "%myapp.controller.base.class%"
arguments: ["@doctrine.orm.entity_manager", , "@templating", "@session", "@translator", "@router", "@form.factory"]
創建並在我的控制器,這也是一個服務注入的基本服務。
myapp.controller.locales:
class: "%myapp.controller.locales.class%"
arguments: ["@myapp.controller.base"]
我的基地服務看起來像這樣。
namespace MyApp\<BundleName>\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class BaseController
{
protected $formErrorService;
protected $entityManager;
protected $templating;
protected $session;
protected $translator;
protected $router;
protected $formFactory;
public function __construct($entityManager, EngineInterface $templating, Session $session, $translator, $router, $formFactory)
{
$this->setEntityManager($entityManager);
$this->setTemplating($templating);
$this->setSession($session);
$this->setTranslator($translator);
$this->setRouter($router);
$this->setFormFactory($formFactory);
}
public function setEntityManager($em)
{
$this->entityManager = $em;
}
public function getEntityManager()
{
return $this->entityManager;
}
公共函數setTemplating($模板) { $這 - >模板= $模板; }
public function getTemplating()
{
return $this->templating;
}
public function setSession($session)
{
$this->session = $session;
}
public function getSession()
{
return $this->session;
}
public function setTranslator($translator)
{
$this->translator = $translator;
}
public function getTranslator()
{
return $this->translator;
}
public function setRouter($router)
{
$this->router = $router;
}
public function getRouter()
{
return $this->router;
}
public function setFormFactory($formFactory)
{
$this->formFactory = $formFactory;
}
public function getFormFactory()
{
return $this->formFactory;
}
}
我和我的語言環境控制器這個樣子的
namespace MyApp\<Bundlename>\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
class LocalesController extends BaseController
{
protected $baseService;
public function __construct($baseService)
{
$this->baseService = $baseService;
}
public function indexAction(Request $request)
{
$locales=$this->baseService->getEntityManager()->getRepository('<reponame>')->findAll();
return $this->baseService->getTemplating()->renderResponse('<templatename>', array('locales' => $locales));
}
稱這是一個正確的步驟?我應該在需要此服務的ervery控制器中使用此過程嗎?
我希望我可以得到有益的啓示,非常感謝