在ZF2中,我有2個助手。助手有一個表單和一個映射器來處理數據庫交互。我使用控制器工廠將這些助手傳遞給控制器。控制器處理一個人或一個組織的一方的電話和地址。因爲參與方是個人或組織,所以它具有不同的數據,所以控制器工廠還將對象:PersonObject或OrganizationObject與參與方特定的數據傳遞到控制器中。ZF2:從控制器工廠更改視圖腳本
2個助手對於雙方類型都是相同的。但在視圖腳本中,我想顯示特定於當事人的數據,這裏是我的問題:我需要根據控制器工廠傳遞給控制器的對象來更改視圖腳本。我認爲有兩個不同的控制器,但它是一個矯枉過正的視圖腳本是90%是相同的,除了從數據庫進入黨對象的10%特定於黨的信息。
如何從控制器工廠更改視圖腳本?通過改變這裏,我的意思是略微不同的HTML佈局與黨的具體數據。
編輯:
@Saeven建議發佈一些代碼。目前,我決定在控制器工廠中創建ViewModel
,並做好相應準備,並將其注入到控制器中。但我不確定這是一件好事。
助手:
class ContactMechanismRegistrationViewHelper extends AbstractRegistrationViewHelper
{
public function __construct(
FormInterface $form,
ContactMechanismMapperInterface $contactMechanismMapper
) {
$this->form = $form;
$this->mapper = $contactMechanismMapper;
}
public function saveToDb()
{
$this->mapper->save(
$this->form->get('contactMechanismFieldset')->getObject(),
$this->form->get('partyFieldset')->getObject()
);
}
}
助手工廠:使用輔助
class ContactMechanismRegistrationViewHelperFactory implements FactoryInterface, MutableCreationOptionsInterface
{
use MutableCreationOptionsTrait;
public function createService(ServiceLocatorInterface $serviceLocator)
{
$serviceManager = $serviceLocator->getServiceLocator();
$formElementManager = $serviceManager->get('FormElementManager');
if (in_array('phone', $this->creationOptions)) {
return new ContactMechanismRegistrationViewHelper(
$formElementManager
->get('Parties\Forms\Forms\ContactMechanisms\PhoneRegistrationForm'),
$serviceManager
->get('Parties\Mappers\ContactMechanisms\PhoneMapper')
);
} elseif (in_array('address', $this->creationOptions)) {
return new ContactMechanismRegistrationViewHelper(
$formElementManager
->get('Parties\Forms\Forms\ContactMechanisms\AddressRegistrationForm'),
$serviceManager
->get('Parties\Mappers\ContactMechanisms\AddressMapper')
);
} else {
throw new ServiceNotCreatedException('wrong option type specified');
}
}
}
控制器:
class PartyDetailsController extends AbstractActionController
{
protected $phoneViewHelper;
protected $addressViewHelper;
protected $partyViewModel;
public function __construct(
ContactMechanismRegistrationViewHelper $phoneViewHelper,
ContactMechanismRegistrationViewHelper $addressViewHelper,
ModelInterface $viewModel
)
{
$this->phoneViewHelper = $phoneViewHelper;
$this->addressViewHelper = $addressViewHelper;
$this->viewModel = $viewModel;
}
public function indexAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$viewHelperForFormSubmission = $this->getViewHelperForFormSubmission(
$request->getPost('submitButton')
);
$viewHelperForFormSubmission->getForm()->setData($request->getPost());
$viewHelperForFormSubmission->getForm()->setIsSubmitted(true);
if ($viewHelperForFormSubmission->getForm()->isValid()) {
try {
$viewHelperForFormSubmission->saveToDb();
$viewHelperForFormSubmission->getForm()->resetForm();
} catch (\Exception $e) {
die($e->getMessage());
}
} else {
$viewHelperForFormSubmission->getForm()->highlightInvalidElements();
}
}
return $this->viewModel->setVariables([
'phoneForm' => $this->phoneViewHelper->getForm(),
'addressForm' => $this->addressViewHelper->getForm(),
]);
}
protected function getViewHelperForFormSubmission($submitValue)
{
if ($submitValue == 'phone') {
return $this->phoneViewHelper;
} elseif ($submitValue == 'address') {
return $this->addressViewHelper;
} else {
throw new \Exception('invalid submit argument');
}
}
}
請包括你的助手的代碼,以及你如何使用它。 – Saeven
@Saeven,嘿,謝謝你的回覆。問題在於它會是很多難以遵循的代碼。這就是爲什麼我用言語保持解釋。 –
只需發佈您的助手和任何構建它的工廠。你的代碼會比你的段落多出100倍:) – Saeven