AuthController在另一个模块中

时间:2013-11-24 11:51:02

标签: php zend-framework2

我在zf2中使用身份验证:

这是一个控制器

<?php

namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Form\Annotation\AnnotationBuilder;
use Zend\View\Model\ViewModel;
use Admin\Model\User;

class AuthController extends AbstractActionController {

    protected $form;
    protected $storage;
    protected $authservice;

    public function getAuthService() {
        if (!$this->authservice) {
            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }

        return $this->authservice;
    }

    public function getSessionStorage() {
        if (!$this->storage) {
            $this->storage = $this->getServiceLocator()->get('Admin\Model\MyAuthStorage');
        }

        return $this->storage;
    }

    public function getForm() {
        if (!$this->form) {
            $user = new User();
            $builder = new AnnotationBuilder();
            $this->form = $builder->createForm($user);
        }

        return $this->form;
    }

    public function loginAction() {
        //if already login, redirect to success page 
        if ($this->getAuthService()->hasIdentity()) {
            return $this->redirect()->toRoute('success');
        }

        $form = $this->getForm();

        return array(
            'form' => $form,
            'messages' => $this->flashmessenger()->getMessages()
        );
    }

    public function authenticateAction() {
        $form = $this->getForm();
        $redirect = 'login';

        $request = $this->getRequest();
        if ($request->isPost()) {
            $form->setData($request->getPost());
            if ($form->isValid()) {
                //check authentication...
                $this->getAuthService()->getAdapter()
                        ->setIdentity($request->getPost('username'))
                        ->setCredential($request->getPost('password'));

                $result = $this->getAuthService()->authenticate();
                foreach ($result->getMessages() as $message) {
                    //save message temporary into flashmessenger
                    $this->flashmessenger()->addMessage($message);
                }

                if ($result->isValid()) {
                    $redirect = 'success';
                    //check if it has rememberMe :
                    if ($request->getPost('rememberme') == 1) {
                        $this->getSessionStorage()
                                ->setRememberMe(1);
                        //set storage again
                        $this->getAuthService()->setStorage($this->getSessionStorage());
                    }
                    $this->getAuthService()->setStorage($this->getSessionStorage());
                    $this->getAuthService()->getStorage()->write($request->getPost('username'));
                }
            }
        }

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

    public function logoutAction() {
        if ($this->getAuthService()->hasIdentity()) {
            $this->getSessionStorage()->forgetMe();
            $this->getAuthService()->clearIdentity();
            $this->flashmessenger()->addMessage("Logout done.");
        }

        return $this->redirect()->toRoute('login');
    }

}

当我尝试在另一个模块“Posts for example”中使用此控制器时,它会返回一条错误消息:

使用

use Admin\Controller\AuthController;

public function indexAction() {

    $getAuth = new AuthController();

    $getAuth->getServiceLocator()->get('AuthService')->hasIdentity();

    $authService = $getAuth->getAuthService();
    if ( $authService->hasIdentity() ) {
        echo 'Auth area';
    }
}

错误消息返回:

  

致命错误:在非对象中调用成员函数get()   第31行..... \ Controller \ PostsController.php

     

第31行意味着:   $ getAuth-&GT; getServiceLocator() - &GT;获得( 'AuthService') - &GT; hasIdentity();

有什么问题?!

1 个答案:

答案 0 :(得分:1)

服务定位器未在AuthController中注入,因此您无法在getServiceLocator()对象上调用$getAuth函数,导致get()函数无效。< / p>

使用$this->getServiceLocator()->get('AuthService')->hasIdentity();

正如Tomdarkness所说的不好的做法,只是想告诉你如何修复你的代码以及糟糕的事情。

查看此链接getting dependencies into zf2 controllers如何对服务定位器进行构造函数注入。