我的工作在這裏我們使用DependencyInjection
項目視圖定義的,所以我必須在src\Common\CommonBundle\Resources\config\services.yml
定義如下:傳遞多種形式對不同業務的Symfony2
services:
address.form:
class: Wuelto\Common\CommonBundle\Controller\FormAddressController
arguments: [@form.factory, @doctrine.orm.entity_manager]
address_extra_info.form:
class: Wuelto\Common\CommonBundle\Controller\FormAddressExtraInfoController
arguments: [@form.factory, @doctrine.orm.entity_manager]
而且src\Company\RegisterCompanyBundle\Resources\config\services.yml
services:
registercompany.form:
class: Wuelto\Company\RegisterCompanyBundle\Controller\FormRegisterCompanyController
arguments: [@form.factory, @doctrine.orm.entity_manager]
這是控制器背後的代碼(其中一個與其他類相同):
class FormAddressExtraInfoController {
public function __construct(FormFactoryInterface $formFactory, EntityManager $em) {
$this->formFactory = $formFactory;
$this->em = $em;
}
private function getEntity($id) {
$entity = new AddressExtraInfo();
try {
if (isset($id)) {
$entity = $this->em->getRepository("CommonBundle:AddressExtraInfo")->find($id);
}
} catch (\Exception $e) {
}
return $entity;
}
public function getAction($id = null) {
$entity = $this->getEntity($id);
$form = $this->formFactory->create(new AddressExtraInfoType($id), $entity, array('method' => 'POST'));
return array('formAddressExtraInfo' => $form->createView());
}
}
所以問題就出在這裏。在另一個控制器(\Website\FrontendBundle\Controller\sellerController.php
)的束之外我想利用這段代碼來獲得$formXXX
觀點:
$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array($this->get('registercompany.form')->getAction(), $this->get('address_extra_info.form')->getAction()));
但我得到這個錯誤:
Variable "formCompany" does not exist in FrontendBundle:Seller:newCompany.html.twig at line 10
的原因是什麼?我不是傳遞值應該是驚奇,但如果我通過他們爲:
$this->render('FrontendBundle:Seller:newSellerLayout.html.twig', array('formCompany' => $this->get('registercompany.form')->getAction(), 'formAddressExtraInfo' => $this->get('address_extra_info.form')->getAction()));
然後誤差變換成這樣:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, array given
我不知道如何解決這個問題或者我做錯了什麼?