Forked from settermjd/zf2-constructor-Injection.php
Last active
August 29, 2015 14:22
-
-
Save cerdobot/893bec16bcc1a90a25a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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