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

时间:2014-04-21 12:27:08

标签: 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 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中,我在工厂下添加了以下代码:

                '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;
}

我已经尝试了很多事情来实现这一目标,我只是不知道该怎样再尝试..

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:-2)

嘿,把它放在你的控制器中:

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