Zend framework2 尝试使用ZfcUser\Service\User::register方法从另一个控制器注册用户时发生ZF2错误

Zend framework2 尝试使用ZfcUser\Service\User::register方法从另一个控制器注册用户时发生ZF2错误,zend-framework2,zfcuser,servicemanager,Zend Framework2,Zfcuser,Servicemanager,我想使用ZfcUser注册一个用户,并在一个表单中向数据库添加一个模型。 因此,我扩展了注册表,并通过将postdata提供给不同的表单来验证数据,如下所示: namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\Form; use Zend\View\Model\ViewModel; use Application\Model\Author; use Appl

我想使用ZfcUser注册一个用户,并在一个表单中向数据库添加一个模型。 因此,我扩展了注册表,并通过将postdata提供给不同的表单来验证数据,如下所示:

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form;
use Zend\View\Model\ViewModel;

use Application\Model\Author;
use Application\Form\AuthorForm;
use Application\Form\Author2Form;

use ZfcUser\Service\User as UserService;

class AdminController extends AbstractActionController {
    protected $authorTable;
    protected $registerForm;
    protected $userService;

    public function authorAction() {
        $author = new Author();
        $form = $this->getRegisterForm();
        $formRegister = $this->getServiceLocator()->get('zfcuser_register_form');

        $formAuthor = new Author2Form();
        $request = $this->getRequest();
        $service = $this->getUserService();

        if ($request->isPost()) {
            $post = $request->getPost();
            $post['email'] = $post['mail'];
            $request->setPost($post);

            $formAuthor->setValidationGroup(array('name', 'last_name', 'mail', 'group_name', 'description'));
            $formRegister->setValidationGroup(array('email', 'password', 'passwordVerify'));
            $form->setData($request->getPost());
            $formAuthor->setData($request->getPost());
            $formRegister->setData($request->getPost());

            if ($formAuthor->isValid() && $formRegister->isValid()) {
                $service->register($formRegister->getData());
                $author->exchangeArray($formAuthor->getData());
                $this->getAuthorTable()->saveAuthor($author);

                return $this->redirect()->refresh();
            }
        }

        return new ViewModel(array(
            'authorList' => $this->getAuthorTable()->fetchAll(),
            'authorForm' => $form,
        ));
    }

    public function getRegisterForm() {
        if (!$this->registerForm) {
            $this->setRegisterForm($this->getServiceLocator()->get('Application\Form\AuthorForm'));
        }
        return $this->registerForm;
    }

    public function setRegisterForm(AuthorForm $registerForm) {
        $this->registerForm = $registerForm;
    }

    public function getUserService() {
        if (!$this->userService) {
            $this->userService = new UserService();
        }
        return $this->userService;
    }

    public function setUserService(UserService $userService) {
        $this->userService = $userService;
        return $this;
    }

}
在Module.php中,我在Factorys下添加了以下代码:

                'Application\Form\AuthorForm'     => function($sm) {
                    $options = $sm->get('zfcuser_module_options');
                    $form = new AuthorForm('authorForm', $options);
                    return $form;
                },
我能够验证这两个表单,然后出现以下错误:

致命错误:对中的非对象调用成员函数get /var/www/*/vendor/zf commons/zfc user/src/ZfcUser/Service/user.php 在线241

这将是以下方法:

    public function getOptions() {
    if (!$this->options instanceof UserServiceOptionsInterface) {
        $this->setOptions($this->getServiceManager()->get('zfcuser_module_options'));
    }
    return $this->options;
}
我已经尝试了很多事情来实现这一点,我只是不知道该尝试什么了


有人能帮我吗?

嘿,把这个放在你的控制器里:

$service->setServiceManager($this->getServiceLocator());