首先,我不确定什么是最好的方法,但我想到了一些。
我认为在您的管理员中加载注册表单会更容易,记住您可以使用服务管理器从任何控制器加载它,例如
$form = $sm->get('zfcuser_register_form');
然后您可以像处理任何表单一样使用它,将其发送到视图等等。
您将拥有完整的注册表单,其中包含您在 zfcuser.global.php 中设置的所有字段,包括密码。我认为最好设置一个临时密码,然后让用户更改它。在第一次更改密码之前,您也可以将其状态设为未确认。
如果您不想要某个特定字段,您可以像使用任何表单一样将其取出,通过
$form->remove('element_name');
您需要检查 ZfcUser\Form\Register
中的元素名称
另外,请记住,如果您删除任何字段,则必须修改输入过滤器,否则验证将失败。为此,在您的模块的引导程序中,您应该附加一个事件侦听器,如下所示:
$em = $e->getApplication ()->getEventManager ();
$em->attach ( 'ZfcUser\Form\RegisterFilter', 'init', function ($e) {
$filter = $e->getTarget ();
//now modify the inputfilter as you need
});
然后,您必须将邮件发送给用户。为此,我还将使用事件管理器,在您的引导程序中,您在创建用户时注册一个侦听器,这是通过
$sm = $e->getApplication ()->getServiceManager ();
$zfcServiceEvents = $sm->get ( 'zfcuser_user_service' )->getEventManager ();
$zfcServiceEvents->attach ( 'register.post', function ($e) {
$form = $e->getParam ( 'form' );
$user = $e->getParam ( 'user' );
//now you have all the info from the form and the already created user, so you can send the mail and whatever you need.
最后一步,是让用户更改他的密码。为此,我会将他发送到您显示更改密码表单的模块,您可以通过以下方式检索:
$sm->get('zfcuser_change_password_form');
或直接将他发送到 /user/change-password 网址,该网址是 zfc-user 预定义的网址之一。
我认为这将是最干净的方式。
另一种方法
如果您不喜欢这种方式,您可以使用另一种方法,您创建自己的表单,填写表格,将数据保存到临时表,发送邮件,然后...当用户来设置他的密码,您构建一个注册表单,预先填写字段(并隐藏,将输入类型更改为隐藏,或通过 css)并让他发送表单,所以当他认为他只是发送密码,其实他是把所有的注册表单都发过来了,从这里开始一切都和正常注册一样。
对于此解决方案,您还必须使用事件,但可能您必须查看 注册事件,该事件在发送表单时触发,在用户之前保存在数据库中,因此您可以修改您可能需要的任何数据。
$zfcServiceEvents->attach ( 'register', function ($e) {
$form = $e->getParam ( 'form' );
您还应该查看已经提到的 init 事件,您可以在向用户显示表单之前检索表单,并预填充临时表中的任何数据。
$events->attach ( 'ZfcUser\Form\Register', 'init', function ($e) {
$form = $e->getTarget ();
//now you set form element values from the temp table
可能这很令人困惑,但我希望你至少知道从哪里开始!