Model view controller 在Zend Framework 2中设置视图插件的最佳方法是什么?
我正在尝试设置一个视图插件来公开Zend Framework 2中的路由匹配。 插件如下所示:Model view controller 在Zend Framework 2中设置视图插件的最佳方法是什么?,model-view-controller,zend-framework,plugins,view,zend-framework2,Model View Controller,Zend Framework,Plugins,View,Zend Framework2,我正在尝试设置一个视图插件来公开Zend Framework 2中的路由匹配。 插件如下所示: class GetRouteMatch extends AbstractHelper { /** * Route match returned by the router. * * @var RouteMatch. */ protected $routeMatch; /** * Set route match returned by
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对象是手动注入的。。但是我确信有更好的方法,最好的方法是在模块类中初始化这样的代码。您可以在那里附加事件以注入依赖项,如routematch。不过,routematch将很快投入使用。正在进行在服务定位器中设置路由匹配的工作。这意味着您可以配置DI,它将从服务定位器中提取路由匹配。那么你就不必再自己写这些东西了。我认为这是一种优雅的方式。。