redirect - zf2中如何确定当前路径和参数

标签 redirect routes zend-framework2

在我正在开发的表单中,如果按下名为“重置”的提交按钮,我想放弃更改并重定向到相同的路由。以下代码用于确定当前 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);

但是,这需要确定当前的路线和参数。我怎样才能做到这一点?

最佳答案

为了回答您的问题,当前匹配的路由名称可在 Controller 中的 MVC 事件中使用:

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

匹配的路由参数:

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

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

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

关于redirect - zf2中如何确定当前路径和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28051782/

相关文章:

javascript - 如何在 PHP 中将页面重定向到不带井号 (#) 的 URL?

c - 将标准输出重定向到文件描述符似乎不起作用,为什么?

javascript - 导致 404 的基本 nodejs 路由

php - 如何使用 PHP Doctrine 2 ORM 在 MySQL varbinary 列上进行选择

api - ZF2 with PhlyRestfully The requested controller was unable to dispatch the request

python - 如何从根路径自动重定向?

python - 如何使用Python检查url是否重定向到另一个url

ruby-on-rails - 基于不同于 ID 的东西的 Rails Route

ruby-on-rails - 为什么参数带点的路由匹配失败?

validation - 如何在 Zend 框架 2 中使用没有表单类的 InputFilter