在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