zf3 – get the route match in a custom view helper

Martin Zeller php, zf3 1 Comment

In a very old post I told you how to get the route name in custom view helper with zf2 – Zend Framework 2. In this post I used the ServiceLocator, a thing which was some kind of deprecated in later versions of zf2. It is bad practice to use such a construct like the getServiceLocator method, because e.g. applications became very bad to unit test – you can find a lot of articles regarding this in the net.

With later zf2 versions and zf3 there are better ways to get the route name in your view helper. One way is to make use of ViewHelperProviderInterface in your src/Module.php class:

That’s all. The basic code of the YourViewHelper view helper could look like this:

Have fun.

Comments 1

  1. Pingback: zf2 view helper get route name (router) | Freelancer Martin Zeller - php java angularjs react fullstack

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.