1 zend-framework-modules zend-framework2
伙计们,此时我已经接近开始拔头发了.我找不到实现这个目标的方法.
我有一个自定义类,属于我在WebServices Module src文件夹下创建的自定义文件夹.我需要能够从另一个模块/控制器内部实例化这个类,但是当我这样做并转储服务成员时,它包含null.如何从ApiAuthentication类中访问服务管理器.
任何帮助将不胜感激.谢谢
<?php
namespace WebServices\Services;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ApiAuthenticationService extends \Zend\Soap\Client implements ServiceLocatorAwareInterface{
public $services;
function __construct($options = null){
parent::__construct('http://tinysoa.local/soap/security/api_authentication?wsdl',$options);
}
public function setServiceLocator(ServiceLocatorInterface $locator)
{
$this->services = $locator;
}
public function getServiceLocator()
{
return $this->services;
}
}
Run Code Online (Sandbox Code Playgroud)
当我从另一个模块/控制器内部调用它时,它转储一个空值:
class IndexController extends AbstractActionController
{
public function indexAction()
{
$a = new \WebServices\Services\ApiAuthenticationService();
var_dump($a->services);
Run Code Online (Sandbox Code Playgroud)
回应我自己对Adrian's附加组件的回答,以及你回答的问题.
如果您的服务具有自己的依赖关系,那么您只需使用工厂而不是使用可调用路由.
假设您的服务需要缓存适配器和数据库适配器.还可以想象它可以选择配置一些其他服务(FooService,如下):
<?php
public function getServiceConfig()
{
return array(
'factories' => array(
'my_service' => function($sm){
$cache = $sm->get('Cache');
$dbAdapter = $sm->get('DefaultDbAdapter');
$fooService = $sm->get('FooService');
// instantiate your service with required dependencies
$mySvc = new \My\Shiny\Service($cache, $dbAdapter);
// inject an optional dependency
$mySvc->setFooService($fooService);
// return your shiny new service
return $mySvc;
}
)
);
}
Run Code Online (Sandbox Code Playgroud)
附注:在整个地方注入ServiceManager通常是糟糕的设计.如上所述,您最好更明确地管理依赖项.
如果您还没有阅读过,那么在快速入门中可以很好地涵盖这些内容.
| 归档时间: |
|
| 查看次数: |
4363 次 |
| 最近记录: |