我有一个认证模块。现在,我希望确保每个模块都通过(与该身份验证模块的通信)。我想您可以说这是对整个应用程序的身份验证。我怎样才能做到这一点?
发布于 2014-08-27 23:31:47
一个简单的方法是通过名称空间获得模块/模块类,然后只需对类进行扩展。在父类中自动调用该功能或在子类中调用该方法。这将是一种非常基本的方法:
// 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文件中的服务配置中为其编写配置。做这件事有很多种方法,ZF2提供了很多这样的选择。
zf2:
// 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
},
)
);
}https://stackoverflow.com/questions/25537291
复制相似问题