2
votes

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!

1

1 Answers

3
votes

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.