【问题标题】:How to determine current route and parameters in zf2zf2中如何判断当前路由和参数
【发布时间】:2015-03-19 01:28:16
【问题描述】:

在我正在开发的表单中,如果按下名为“reset”的提交按钮,我想放弃更改并重定向到相同的路由。以下代码用于确定当前 url 和自重定向:

$hereandnow = $this->getRequest()->getRequestUri();
return $this->redirect()->toUrl($hereandnow);

我想用路由方法做同样的事情:

return $this->redirect()->toRoute($current_route, $current_params);
     // OR
return $this->redirect()->toRoute($current_route_including_params);

但是,这需要确定当前路线和参数。我该怎么做?

【问题讨论】:

  • 为什么不使用type="reset" 输入按钮?
  • type="reset" 输入按钮有效,但我还需要一个不关闭表单的“保存”按钮的解决方案。

标签: redirect routes zend-framework2


【解决方案1】:

为了回答你的问题,当前匹配的路由名称在 MVC 事件中可用,在控制器中:

$this->getEvent()->getRouteMatch()->getMatchedRouteName();

匹配的路由参数:

$this->getEvent()->getRouteMatch()->getParams();

但是,您可以通过更简单的方式重定向到相同的 URL(刷新页面):

return $this->redirect()->refresh();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多