zf3 – get the route match in a custom view helper

Martin Zellerphp, zf3 2 Comments

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:

<?php

namespace Application;

use Application\View\Helper\YourViewHelper;
use Zend\ModuleManager\Feature\ViewHelperProviderInterface;
use Zend\ServiceManager\ServiceManager;

/**
 * Class Module
 * @package Application
 */
class Module implements ViewHelperProviderInterface
{
    /**
     * Expected to return \Zend\ServiceManager\Config object or array to
     * seed such an object.
     *
     * @return array|\Zend\ServiceManager\Config
     */
    public function getViewHelperConfig()
    {
        return [
            'factories' => [
                'yourViewHelper' => function(ServiceManager $serviceManager) {
                    return new YourViewHelper($serviceManager->get('Application')->getMvcEvent()->getRouteMatch()); // <---- this is the important part
                }
            ],
        ];
    }
}

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

<?php

namespace Application\View\Helper;


use Zend\Router\Http\RouteMatch;
use Zend\View\Helper\AbstractHelper;


/**
 * Class YourViewHelper
 * @package Application\View\Helper
 */
class YourViewHelper extends AbstractHelper
{

    /**
     * @var RouteMatch
     */
    private $routeMatch;

    /**
     * YourViewHelper constructor.
     * @param RouteMatch $routeMatch
     */
    public function __construct(RouteMatch $routeMatch) {
        $this->routeMatch = $routeMatch;
    }

    /**
     * @return string
     */
    public function __invoke()
    {

        $route = $this->getRouteName();

        // ... do something with your view helper

    }

   /**
     * @return RouteMatch
     */
    protected function getRouteMatch()
    {
       return $this->routeMatch;
    }

    /**
     * @return string|null
     */
    protected function getRouteName()
    {
        return $this->getRouteMatch()->getMatchedRouteName();
    }

    /**
     * @param $param
     * @return mixed|null
     */
    protected function getRouteParam($param)
    {
        return $this->getRouteMatch()->getParam($param, '');
    }

}

Have fun.

Comments 2

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

  2. Not a good idea because __construct will fail on routes that are not matched… triggering a nice 500 error even on 404 pages…

Leave a Reply

Your email address will not be published. Required fields are marked *

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