Last active
August 29, 2015 14:07
-
-
Save fabiopaiva/f0edb03b091aafe2abbc to your computer and use it in GitHub Desktop.
Assign serviceLocator to a custom view helper in Zend Framework 2
This file contains 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
<?php | |
namespace Application; | |
class Module{ | |
public function getViewHelperConfig() { | |
return array( | |
'factories' => array( | |
'myHelper' => function($sm){ | |
return new \Application\View\Helper\MyHelper($sm->getServiceLocator()); | |
} | |
) | |
); | |
} | |
} |
This file contains 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
My helper do: <?php echo $this->myHelper();?> |
This file contains 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
<?php | |
namespace Application\View\Helper; | |
use Zend\View\Helper\AbstractHelper; | |
class MyHelper extends AbstractHelper { | |
/** | |
* | |
* @var \Zend\ServiceManager\ServiceManager | |
*/ | |
private $sm; | |
public function __construct(\Zend\ServiceManager\ServiceManager $sm) { | |
$this->sm = $sm; | |
} | |
public function getSm() { | |
return $this->sm; | |
} | |
public function __invoke() { | |
$html = 'print this'; | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment