可以使用Go轻松模拟Laravel的return Redirect::back()行为吗?

huangapple go评论115阅读模式
英文:

Can Go easily emulate the behaviour of Laravel's return Redirect::back()?

问题

在使用Laravel时,我非常喜欢能够使用以下代码来返回上一个URL:

  1. return Redirect::back();

这样可以在POST请求后返回到之前的URL。

Go中是否有一种简单的内置方法来实现这个功能呢?

  1. http.Redirect(w, r, backURL, http.StatusSeeOther)

其中backURL是发起POST请求的URL。

我已经查看了net/http并在Stack Exchange和Google上进行了搜索,但没有找到相关信息。如果没有简单的方法,我希望能够得到在Go中实现这个功能的惯用方式的指导。

英文:

While using Laravel, I really appreciated being able to use:

  1. return Redirect::back();

to return to the previous URL after a POST request.

Is there a simple in-built way to do this in Go?

  1. http.Redirect(w, r, backURL, http.StatusSeeOther)

where backURL is the URL that the POST request was made from.

I've looked through net/http and searched SE and google, but I haven't turned anything up. Failing an easy way, I'd appreciate any pointer toward the idiomatic way of doing this in Go.

答案1

得分: 4

Redirect::back()函数使用客户端(浏览器)指定的Referer HTTP头中的URL。

你可以像这样访问这个头的值:r.Header.Get("Referer"),但http.Request类型还提供了一个直接的Request.Referer()方法,返回这个Referer字段的值,同时处理了它的两种可能形式("Referer"和"Referrer")。

以下是模拟“返回上一页”的方法:

  1. func PostHandler(w http.ResponseWriter, r *http.Request) {
  2. // 处理表单,然后:
  3. if rf := r.Referer(); rf != "" {
  4. http.Redirect(w, r, rf, http.StatusSeeOther)
  5. return
  6. }
  7. // 没有指定Referer,提供自己的响应
  8. // 或者重定向到默认/主页
  9. http.Redirect(w, r, "/", http.StatusSeeOther)
  10. }

如果你想从多个处理程序中调用这个功能,可以将其封装在一个辅助函数中:

  1. func redirectBack(w http.ResponseWriter, r *http.Request) {
  2. if rf := r.Referer(); rf != "" {
  3. http.Redirect(w, r, rf, http.StatusSeeOther)
  4. return
  5. }
  6. // 没有指定Referer,提供自己的响应
  7. // 或者重定向到默认/主页
  8. http.Redirect(w, r, "/", http.StatusSeeOther)
  9. }

然后在使用时调用它:

  1. func PostHandler(w http.ResponseWriter, r *http.Request) {
  2. // 处理表单,然后:
  3. redirectBack(w, r)
  4. }
  5. }
  6. [1]: https://golang.org/pkg/net/http/#Request
  7. [2]: https://golang.org/pkg/net/http/#Request.Referer
英文:

The Redirect::back() function uses the URL from the Referer HTTP header specified by the client (browser).

You could access this header value like r.Header.Get("Referer"), but the http.Request type also provides a direct Request.Referer() method that returns the value of this Referer field, which also deals with its 2 possible forms ("Referer" and "Referrer").

This is how you can mimic the "go back" behavior:

  1. func PostHandler(w http.ResponseWriter, r *http.Request) {
  2. // Process form, then:
  3. if rf := r.Referer(); rf != "" {
  4. http.Redirect(w, r, rf, http.StatusSeeOther)
  5. return
  6. }
  7. // No Referer specified, supply your own response
  8. // or redirect to a default / home page
  9. http.Redirect(w, r, "/", http.StatusSeeOther)
  10. }

If you want to call this from many handlers, you may capture this functionality in a helper function:

  1. func redirectBack(w http.ResponseWriter, r *http.Request) {
  2. if rf := r.Referer(); rf != "" {
  3. http.Redirect(w, r, rf, http.StatusSeeOther)
  4. return
  5. }
  6. // No Referer specified, supply your own response
  7. // or redirect to a default / home page
  8. http.Redirect(w, r, "/", http.StatusSeeOther)
  9. }

And then using it:

  1. func PostHandler(w http.ResponseWriter, r *http.Request) {
  2. // Process form, then:
  3. redirectBack(w, r)
  4. }

huangapple
  • 本文由 发表于 2016年11月11日 17:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/40544972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定