Skip to content

Instantly share code, notes, and snippets.

@fabiopaiva
Last active August 29, 2015 14:07
Show Gist options
  • Save fabiopaiva/f0edb03b091aafe2abbc to your computer and use it in GitHub Desktop.
Save fabiopaiva/f0edb03b091aafe2abbc to your computer and use it in GitHub Desktop.
Assign serviceLocator to a custom view helper in Zend Framework 2
<?php
namespace Application;
class Module{
public function getViewHelperConfig() {
return array(
'factories' => array(
'myHelper' => function($sm){
return new \Application\View\Helper\MyHelper($sm->getServiceLocator());
}
)
);
}
}
My helper do: <?php echo $this->myHelper();?>
<?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