我正在尝试设置一个视图插件来公开 Zend Framework 2 中的路由匹配。插件是这样的:
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);
}
}
设置 RouteMatch 对象的最佳方法是什么?我必须在模块引导程序或控制器中进行吗?
目前我已经在控制器动作中以这种方式解决了
$renderer = $this->getLocator()->get('Zend\View\Renderer\PhpRenderer');
$routeMatch = $renderer->plugin('routeMatch');
$routeMatch->setRouteMatch($this->getEvent()->getRouteMatch());
RouteMatch 对象是手动注入的.. 但我确信有更好的方法