Module zend framework 2中模块之间的通信
我有一个身份验证模块。现在,我想确保每个模块都通过(通信)该身份验证模块。我想你可以说这是对整个应用程序的身份验证。如何实现这一点?一个简单的方法是通过名称空间获取模块/module_类,然后您可以扩展该类。在父类中自动调用功能,或在子类中调用方法。这将是一种非常基本的方式:Module zend framework 2中模块之间的通信,module,zend-framework2,Module,Zend Framework2,我有一个身份验证模块。现在,我想确保每个模块都通过(通信)该身份验证模块。我想你可以说这是对整个应用程序的身份验证。如何实现这一点?一个简单的方法是通过名称空间获取模块/module_类,然后您可以扩展该类。在父类中自动调用功能,或在子类中调用方法。这将是一种非常基本的方式: // Auth class class SomeAuthClass { public function __construct() { // go ahead and call doAuth
// Auth class
class SomeAuthClass
{
public function __construct()
{
// go ahead and call doAuthCrap here, or wait
// and let the child class call it manually
}
protected function doAuthCrap()
{
// code
}
}
use Your\AuthModule\SomeAuthClass;
class SomeOtherModuleClass extends SomeAuthClass
{
public function zippy_snipp()
{
// call some method from the parent auth class (doAuthCrap)
}
}
或者,为了遵循ZF2的一些新方法,您可以通过服务管理器访问auth类,并在module.php文件的service config中为其编写配置。做这件事有多种方法,ZF2提供了很多这样的选择
zf2:
所以我现在需要为每个模块和控制器做这个?这似乎很荒谬。应该有一种更简单的方法来处理这个问题。如果每个类都在扩展某个基类,并且基类中的方法已经在初始化时被调用,那么这并不是那么荒谬,你所做的只是子类化。你也研究过依赖注入吗?哦,我明白了。我没有读对。我将尝试子类化。什么是依赖注入?我无法从基本控制器访问工厂。我在默认应用程序模块中设置了工厂。那不是全站点都有吗?它返回以下错误:
调用非对象上的成员函数get()
您的控制器是否扩展了Zend\Mvc\controller\AbstractActionController?该类扩展了服务定位器所在的类,该类位于Zend\Mvc\Controller\AbstractController中
// in controller
$auth = $this->getServiceLocator()->get('someAuth');
// in service config in module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'someAuth' => function ($serviceManager) {
// code here
},
)
);
}