I'm trying to setup a view plugin to expose the route matches in Zend Framework 2. The plugin is something like this:
class GetRouteMatch extends AbstractHelper
{
/**
* Route match returned by the router.
*
* @var RouteMatch.
*/
protected $routeMatch;
/**
* Set route match returned by the router.
*
* @param RouteMatch $routeMatch
* @return self
*/
public function setRouteMatch(RouteMatch $RouteMatch)
{
$this->routeMatch = $RouteMatch;
return $this;
}
public function __invoke($param)
{
return $this->routeMatch->getParam($param, false);
}
}
What is the best way to setup the RouteMatch object? I have to do it in the module bootstrap or in the controller?
For the moment I've resolved this way inside the controller action
$renderer = $this->getLocator()->get('Zend\View\Renderer\PhpRenderer');
$routeMatch = $renderer->plugin('routeMatch');
$routeMatch->setRouteMatch($this->getEvent()->getRouteMatch());
The RouteMatch object is injected manually.. but I'm sure there's a better way