2

Does anyone know how to pass an instance of a custom Service Manager between 2 actions? As the Zend Framework 2 documentation says, a SM will preserve it's instance if the 'shared' option inside the Module.php class will not be set to false. However, getting the service manager via $manager = $this->getServiceLocator()->get('MyServiceManager'); in different actions will return different instances of the MyServiceManager class. What I want to achieve is: I use an API call to a third party service, which obviously returns a response with various information data, however, if the user heads to another action/page where the same data returned previously from the API request is needed would be nice to save it as a MyServiceManager property and being accessed whenever needed from the class instance if it's set rather that sending another API request every time.

If this is possible, I would be more than happy to listen and learn!

Andrei Stalbe
  • 1,511
  • 6
  • 26
  • 44

1 Answers1

3

The request life cycle (using mod_php, fastcgi/fpm) usually prevent sharing resources. Unlike languages running on application servers, PHP has no built-in way of sharing object instances.

For each incoming request, the web server creates a "new" instance of the php runtime, executes the request and clean itself up.

You could use APC to serialize object instances or memcache to temporarily store results between requests.

Martin Samson
  • 3,970
  • 21
  • 25
  • Zend\Cache provides handy adapters for various caching back-ends. – timdev Jun 14 '13 at 20:20
  • This is the thing about Zend's Service managers, it's instance is saved in some kind of cache according to their documentation. Unfortunately it doesn't work in my case. – Andrei Stalbe Jun 14 '13 at 20:28
  • Sounds like you want to cache the data returned by the API call, not the service manager instance – Tim Fountain Jun 14 '13 at 22:33
  • Tim, the service manager is the class that makes the API request, the returned data is parsed, filtered and divided into setters and getters, when the user gets to another page, the data returned previously from the API call will be extracted from the service manager instance via the `get` magic methods. So yes, I need to cache the instance of the service manager. – Andrei Stalbe Jun 15 '13 at 06:56
  • 1
    Why is the service manager making an API request? It's not clear what you mean, but even though I don't know exactly what you mean, it seems dumb. How is "making a call to remote API" in any way related to "managing services" (which is what you'd expect a 'ServiceManager' to do)? – timdev Jun 15 '13 at 07:59
  • "service manager is the class that makes the API request, the returned data is parsed, filtered". Why not save the resulting data into memcache/APC? – Martin Samson Jun 17 '13 at 12:48