如何重定向到 Zend 框架 2 中的上一页?

问题描述:

对于这些情况,我想自动重定向回我发出请求的页面:

For these situations, I want to auto redirect back to the pages from where I made the request:

  1. 完成任何操作(CRUD 等)后 - 在这里我认为我们需要重定向到HTTP_REFERER"
  2. 在浏览或购物时,如果需要登录,那么在完成身份验证过程后,应该重定向回同一页面

另一种情况(不是重定向到上一页"):

A different situation (which is not 'redirect to previous page'):

  • 在查询中传递重定向 URL(登陆页面地址),例如:如果我发送一个(外部)/URL(ofc 编码)作为 GET 查询参数(或路由部分),登录后,它应该将我重定向到这个网址

我已经在网上搜索过这个并找到了一些解决方案 但它们不符合 Zend Framework 2.我希望所有这些都以 zf2 方式完成.

I have already searched over net for this and found some solutions but they are not according to Zend Framework 2. I want all this to be done in zf2 way.

感谢您的帮助!

嗯,对于 CRUD 的东西,我会简单地重定向到上一个操作的路由,通常类似于 admin-index, administrate 或其他什么.我真的不明白为什么在这种情况下你需要 HTTP_REFERER.但是,如果您仍然想访问 HTTP_REFERER,就这么简单:

Well, for the CRUD stuff, I'd simply redirect to the routes of the previous action, usually something like admin-index, administrate or whatever. I don't really see why you would need the HTTP_REFERER in that case. If however you still want to access the HTTP_REFERER it's as simple as this:

//SomeController.php
$redirectUrl = $this->getRequest()->getHeader('HTTP_REFERER', $defaultValue);

更多信息参见Zend\Http\Request

重定向将使用redirect()>Zend\Mvc\Controller\AbstractActionController

$this->redirect()->toUrl($redirectUrl); //or using a route:
$this->redirect()->toRoute('some/route');

要查看您的用例的更多现场示例,我建议您查看一些非常适合您的用例的模块.这里最合适的可能是 Zf-Commons\ZfcUserbjyoungblood\BjyAuthorize.对于这些示例中的每一个,我都链接了相关的代码示例,这些示例可能会帮助您了解您的需求.

To see some more live examples of your use-cases, i suggest you check out some of the Modules that pretty much fit your use-cases. Probably the most fitting ones here would be Zf-Commons\ZfcUser and bjyoungblood\BjyAuthorize. For each of those examples i have linked relevant code-samples that may shed some insight to your needs.