Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitkorn/7c8ccab3f9fb8ee7d1d10ff5dc54cf58 to your computer and use it in GitHub Desktop.
Save bitkorn/7c8ccab3f9fb8ee7d1d10ff5dc54cf58 to your computer and use it in GitHub Desktop.
Use the ZF2 URL ViewHelper in my ViewHelper
<?php
class Module implements Zend\ModuleManager\Feature\ViewHelperProviderInterface
{
public function getViewHelperConfig()
{
return array(
'factories' => [
'simpleAnchor' => function(\Zend\View\HelperPluginManager $hpm) {
$sm = $hpm->getServiceLocator();
$helper = new View\Helper\W3\SimpleAnchor();
$vhm = $sm->get('viewhelpermanager');
$helper->setUrlHelper($vhm->get('url'));
return $helper;
}
]
);
}
}
class SimpleAnchor
{
/**
*
* @var \Zend\View\Helper\Url
*/
private $urlHelper;
public function __invoke(string $route, string $content, string $routematch = '', string $cssClass = ''): string
{
$currentNavClass = $route == $routematch ? 'currentnav' : '';
$currentNavClass .= !empty($cssClass) ? ' ' . $cssClass : '';
$w3ListItem = "<a class=\"$currentNavClass\"" . ' href="' . call_user_func($this->urlHelper, $route) . '">' . $content . '</a>';
return $w3ListItem;
}
/**
*
* @param \Zend\View\Helper\Url $urlHelper
*/
public function setUrlHelper(\Zend\View\Helper\Url $urlHelper)
{
$this->urlHelper = $urlHelper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment