{"id":148,"date":"2013-04-23T01:18:51","date_gmt":"2013-04-22T23:18:51","guid":{"rendered":"http:\/\/www.zellot.at\/blogs\/?p=148"},"modified":"2013-04-23T01:22:49","modified_gmt":"2013-04-22T23:22:49","slug":"view-helper-routennamen-und-action-in-der-view","status":"publish","type":"post","link":"https:\/\/www.zellot.at\/blogs\/2013\/04\/23\/view-helper-routennamen-und-action-in-der-view\/","title":{"rendered":"View Helper, Routennamen und Action in der View"},"content":{"rendered":"<p>Oft ben\u00f6tigt man in den View &#8211; Files die aktuelle Route, den Controllernamen und den Actionnamen. In Zend2 ist es ganz einfach zu l\u00f6sen. Zuerst brauch man einen View Helper, der einen in den View &#8211; Files zur Verf\u00fcgung steht.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nuse Zend\\View\\Helper\\AbstractHelper;\n\nclass RouteHelper extends AbstractHelper\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;protected $routeMatch;\n\n&nbsp;&nbsp;&nbsp;&nbsp;public function __construct($routeMatch){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;routeMatch = $routeMatch;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;public function __invoke(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public function getControllerName(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($this-&gt;routeMatch != null){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $this-&gt;routeMatch-&gt;getParam(&#039;controller&#039;, &#039;index&#039;);&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#039;&#039;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public function getMatchedRoute(){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($this-&gt;routeMatch != null){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $this-&gt;routeMatch-&gt;getMatchedRouteName(); \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#039;&#039;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public function activeIfMatch($route, $action = null){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($this-&gt;getMatchedRoute() == $route){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($action == null || $this-&gt;routeMatch-&gt;getParam(&#039;action&#039;, &#039;index&#039;) == $action){\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#039;active&#039;;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return &#039;&#039;;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}\n\n<\/code><\/pre><br \/>\nIn der Module Klasse<br \/>\n<pre><code class=\"preserve-code-formatting\">\n\n...\npublic function onBootstrap(MvcEvent $e)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$e-&gt;getApplication()-&gt;getServiceManager()-&gt;get(&#039;translator&#039;);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventManager&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= $e-&gt;getApplication()-&gt;getEventManager();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/view helper for route name\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$e-&gt;getApplication()-&gt;getServiceManager()-&gt;get(&#039;viewhelpermanager&#039;)-&gt;setFactory(&#039;routeHelper&#039;, function($sm) use ($e) {\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$viewHelper = new \\AAUShare\\Helper\\RouteHelper($e-&gt;getRouteMatch());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $viewHelper;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$moduleRouteListener = new ModuleRouteListener();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$moduleRouteListener-&gt;attach($eventManager);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;}\n...\n<\/code><\/pre><\/p>\n<p>Wichtig ist hier vor allem die Methode activeIfMatch welche eine Route und eine Action erwartet. Wenn diese erf\u00fcllt sind so wird active zur\u00fcckgegeben. Dies habe ich vor allem mit den Gedanken gebaut nur mehr diese Methode in der View ausgeben zu m\u00fcssen (z.B. um den Men\u00fcpunkt als active zu Markieren). In der View dann:<br \/>\n<pre><code class=\"preserve-code-formatting\">\n... &lt;li class=&quot;&lt;?php echo $this-&gt;routeHelper()-&gt;activeIfMatch(&#039;home&#039;); ?&gt;&quot;&gt;&lt;a&nbsp;&nbsp;... \n<\/code><\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Oft ben\u00f6tigt man in den View &#8211; Files die aktuelle Route, den Controllernamen und den Actionnamen. In Zend2 ist es ganz einfach zu l\u00f6sen. Zuerst brauch man einen View Helper, der einen in den View &#8211; Files zur Verf\u00fcgung steht. use Zend\\View\\Helper\\AbstractHelper; class RouteHelper extends AbstractHelper { &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;protected $routeMatch; &nbsp;&nbsp;&nbsp;&nbsp;public function __construct($routeMatch){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;routeMatch = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[34,33,35,32],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-php","category-zend-2","tag-module","tag-navigation","tag-onbootstrap","tag-view-helper"],"_links":{"self":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":4,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/posts\/148\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zellot.at\/blogs\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}