Last active
January 4, 2018 10:56
-
-
Save bitkorn/7c8ccab3f9fb8ee7d1d10ff5dc54cf58 to your computer and use it in GitHub Desktop.
Use the ZF2 URL ViewHelper in my ViewHelper
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 | |
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