ZF2在自定义类中获取自动加载的配置信息

时间:2015-01-06 21:11:24

标签: php zend-framework2 apigility

我已经在两天的大部分时间里绞尽脑汁了。我使用Zend Apigility创建RESTful Web API应用程序。 Apigility使用ZF2构建其应用程序。

我创建了一个我在整个API中使用的自定义类。

我想阅读一些自动加载的配置信息以建立与memcache服务器的连接。正在自动加载到服务管理器中的文件是:

memcache.config.local.php:

return array(
  'memcache' => array(
      'server' => '10.70.2.86',
      'port' => '11211',
  ),
);

我的REST服务正在调用的自定义类称为checkAuth:

checkAuth.php:

namespace equiAuth\V1\Rest\AuthTools;

use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class checkAuth implements ServiceLocatorAwareInterface{

    protected $services;

    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
    {
        $this->services = $serviceLocator;
    }

     public function getServiceLocator()
    {
        return $this->services;
    }

    public function userAuths() {
        //** Some Code

       $config = $this->getServiceLocator()->get('config');

        // ** 
    }
}

我相信我正在使用以下代码从我的module.config.php将服务管理器注入到类中:

'service_manager' => array(
    'invokables' => array(
        'checkAuth' => 'equiAuth\V1\Rest\AuthTools\checkAuth',
    ),
 ),

当我尝试阅读' config'从ServiceLocator的get方法我得到以下错误:

  

致命错误:在非对象上调用成员函数get()

我知道我错过了一些东西,但我不知道我的生活会弄清楚什么。

2 个答案:

答案 0 :(得分:0)

为您的类提供一个API,允许您从客户端代码“设置”配置。这可以通过构造函数或 公共制定者。

namespace equiAuth\V1\Rest\AuthTools;

class CheckAuth
{
    protected $config;

    public function __construct(array $config = array())
    {
        $this->setConfig($config);
    }

    public function setConfig(array $config)
    {
        $this->config = $config;
    }

    public function doStuff()
    {
        $server = $this->config['server'];
    }

}

为了“设置”配置,您还需要创建服务工厂类。工厂的想法是为您提供一个区域,将配置注入服务中;通过上面CheckAuth的更新,我们现在可以轻松完成。

namespace equiAuth\V1\Rest\AuthTools;

use equiAuth\V1\Rest\AuthTools\CheckAuth;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class CheckAuthFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('config');

        return new CheckAuth($config['memcache']);
    }
}

最后,使用服务管理器更改注册的服务;此处的更改是服务密钥表单invokablesfactories,因为我们需要注册 以上工厂来创造它。

// module.config.php
'service_manager' => array(
    'factories' => array(
        'checkAuth' => 'equiAuth\V1\Rest\AuthTools\CheckAuthFactory',
    ),
),

答案 1 :(得分:0)

ZF2也使用ServiceManager容器。

你的代码是对的,但是 要在您的班级上自动注入servicelocator,您只需使用

$checkAuth = $this->getServiceLocator()->get('checkAuth');

然后你可以打电话

$checkAuth->userAuths();

并且应该可以工作。

如果您尝试使用:

$checkAuth = new \equiAuth\V1\Rest\AuthTools\checkAuth();
$checkAuth->userAuths(); //error

无法正常工作,因为将serviceLocator注入到您的类中的只是 ServiceManager,一旦你使用serviceManager,你需要与他们一起做传播。

但如果你尝试:

$checkAuth = new \equiAuth\V1\Rest\AuthTools\checkAuth();
$checkAuth->setServiceLocator($serviceLocator) 
//get $serviceLocator from ServiceManager Container
$checkAuth->userAuths();

也会工作。

干得好!