简体   繁体   中英

ZF2 Getting Autoloaded config info in a custom class

I have been racking my brain now for the better part of two days. I'm using Zend Apigility to create a RESTful web API application. Apigility builds its application using ZF2.

I created a custom class that I use throughout my API.

I would like to read in some autoloaded configuration information to make a connection to an memcache server. The file that is being autoloaded into the service manager is:

memcache.config.local.php:

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

My custom class that my REST services are calling is called 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');

        // ** 
    }
}

I believe I'm injecting the service manager into the class from my module.config.php with the following code:

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

When I hit the code when I'm trying to read the 'config' from the get method of the ServiceLocator I get the following error:

Fatal error: Call to a member function get() on a non-object

I know I'm missing something, but I cant for the life of me figure out what.

Give your class an API that allow's you to 'set' the configuration from client code. This could be via the constructor or a public setter.

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'];
    }

}

In order to 'set' the configuration you would also need to also create a service factory class. The idea in the factory is to give you an area to inject the configuration in to the service; with the updates to CheckAuth above we can now do so very easily.

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']);
    }
}

Lastly, change the registered service with the service manager; the change here is service key form invokables to factories as we need to register the above factory to create it.

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

ZF2 use ServiceManager Container as well.

Your code is right at all, but To auto-inject the servicelocator on your class you just need to use

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

then you can call

$checkAuth->userAuths();

and should work.

If you try to use:

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

Will not work because what inject the serviceLocator into your class is just the ServiceManager, once you use serviceManager you need to be evangelist with them.

But if you try:

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

Will work too.

Good job!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM