您好,我正在尝试使用 Zend Framwork 2 的 ZfcUser 模块编写用户注册表单,并且希望获得一些有关添加更多用户字段时最佳实践的建议。
到目前为止,我已经创建了自己的模块,名为“WbxUser”,如模块 wiki 页面 https://github.com/ZF-Commons/ZfcUser/wiki/How-to-modify-the-form-objects-used-by-ZfcUser我通过使用模块引导函数中的事件管理器,向 ZfcUser 的注册表单添加了一个名为“userlastname”的自定义字段,如下所示。
Code:
//WbxUser.Module.php
namespace WbxUser;
use Zend\Mvc\MvcEvent;
class Module {
public function onBootstrap(MvcEvent $e){
$events = $e->getApplication()->getEventManager()->getSharedManager();
$events->attach('ZfcUser\Form\Register','init', function($e) {
$form = $e->getTarget();
$form->add(array(
'name' => 'userlastname',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
// Do what you please with the form instance ($form)
});
$events->attach('ZfcUser\Form\RegisterFilter','init', function($e) {
$filter = $e->getTarget();
$filter->add(array(
'name' => 'userlastname',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
),
));
});
}
public function getConfig(){
return array();
}
public function getAutoloaderConfig(){
return array();
}
}
但在此之后,我有点迷失在哪里/如何编写代码来保存我的新字段正在收集的额外数据。
这是我可以为其他字段触发保存例程的事件吗https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created
我是否应该编写自己的 WbxUser 模型来扩展 ZfcUser\Entity\User.php 以添加我的新字段
我是一个 ZF 和 MVC 菜鸟,所以如果能在写作方向上有所推动,我会非常高兴。