我目前正在从一个名为 ZfcUser for Zend 2 的模块中寻找这段代码:
namespace ZfcUser\Controller;
use Zend\Form\Form;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Stdlib\ResponseInterface as Response;
use Zend\Stdlib\Parameters;
use Zend\View\Model\ViewModel;
use ZfcUser\Service\User as UserService;
use ZfcUser\Options\UserControllerOptionsInterface;
class UserController extends AbstractActionController
{
/**
* @var UserService
*/
protected $userService;
.
.
public function indexAction()
{
if (!$this->zfcUserAuthentication()->hasIdentity()) {
return $this->redirect()->toRoute('zfcuser/login');
}
return new ViewModel();
}
.
.
}
在命名空间 ZfcUser\Controller\Plugin 中:
命名空间 ZfcUser\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
use Zend\Authentication\AuthenticationService;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Authentication\Adapter\AdapterChain as AuthAdapter;
class ZfcUserAuthentication extends AbstractPlugin implements ServiceManagerAwareInterface
{
/**
* @var AuthAdapter
*/
protected $authAdapter;
.
.
/**
* Proxy convenience method
*
* @return mixed
*/
public function hasIdentity()
{
return $this->getAuthService()->hasIdentity();
}
/**
* Get authService.
*
* @return AuthenticationService
*/
public function getAuthService()
{
if (null === $this->authService) {
$this->authService = $this->getServiceManager()->get('zfcuser_auth_service');
}
return $this->authService;
}
我的问题:
- 从 indexAction() 中,控制器插件被调用而不被实例化($this->zfcUserAuthentication()->hasIdentity()),控制器插件总是这样工作吗?
- hasIdentity() 中到底发生了什么?我看到 getAuthService() 返回了一些东西,但没有返回 hasIdentity()。我不熟悉这种函数调用的高级类实现,所以我非常感谢这里的任何解释或我应该研究的主题。