【问题标题】:ZF2 Getting Autoloaded config info in a custom classZF2 在自定义类中获取自动加载的配置信息
【发布时间】:2015-01-06 21:11:24
【问题描述】:

这两天的大部分时间里,我一直在绞尽脑汁。我正在使用 Zend Apigility 创建一个 RESTful Web API 应用程序。 Apigility 使用 ZF2 构建其应用程序。

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

我想读入一些自动加载的配置信息来连接到内存缓存服务器。正在自动加载到服务管理器的文件是:

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',
    ),
 ),

当我尝试从 ServiceLocator 的 get 方法读取“配置”时点击代码时,我收到以下错误:

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

我知道我错过了一些东西,但我一辈子都想不通。

【问题讨论】:

    标签: php zend-framework2 laminas-api-tools


    【解决方案1】:

    为您的班级提供一个 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',
        ),
    ),
    

    【讨论】:

      【解决方案2】:

      ZF2 也使用 ServiceManager Container。

      您的代码完全正确,但是 要在你的类上自动注入服务定位器,你只需要使用

      $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();
      

      也可以。

      干得好!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-13
        • 1970-01-01
        • 2014-01-06
        • 2016-04-17
        • 2014-05-10
        • 1970-01-01
        • 2020-08-02
        • 2022-01-22
        相关资源
        最近更新 更多