Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cerdobot/893bec16bcc1a90a25a1 to your computer and use it in GitHub Desktop.
Save cerdobot/893bec16bcc1a90a25a1 to your computer and use it in GitHub Desktop.
// IndexControllerFactory.php
namespace YourModule\Controller\Factory;
use Zend\ServiceManager\FactoryInterface,
Zend\ServiceManager\ServiceLocatorInterface,
Zend\ServiceManager\Exception\ServiceNotCreatedException,
YourModule\Controller\IndexController;
class IndexControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$sm = $serviceLocator->getServiceLocator();
try {
$cache = $sm->get('Application\Cache');
} catch (ServiceNotCreatedException $e) {
$cache = null;
} catch (ExtensionNotLoadedException $e) {
$cache = null;
}
$controller = new IndexController($cache);
return $controller;
}
}
// IndexController.php
use Zend\Cache\Storage\Adapter\AbstractAdapter;
class IndexController
{
protected $_cache;
public function __construct(AbstractAdapter $cache = null)
{
if (!is_null($cache)) {
$this->_cache = $cache;
}
}
}
// module.config.php
return array(
'controllers' => array(
'factories' => array(
'YourModule\Controller\Index' => 'YourModule\Controller\Factory\IndexControllerFactory',
)
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment