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

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

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

问题

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

return Redirect::back();

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

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

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:

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?

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")。

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

func PostHandler(w http.ResponseWriter, r *http.Request) {
    // 处理表单,然后:
    if rf := r.Referer(); rf != "" {
        http.Redirect(w, r, rf, http.StatusSeeOther)
        return
    }

    // 没有指定Referer,提供自己的响应
    // 或者重定向到默认/主页
    http.Redirect(w, r, "/", http.StatusSeeOther)
}

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

func redirectBack(w http.ResponseWriter, r *http.Request) {
    if rf := r.Referer(); rf != "" {
        http.Redirect(w, r, rf, http.StatusSeeOther)
        return
    }

    // 没有指定Referer,提供自己的响应
    // 或者重定向到默认/主页
    http.Redirect(w, r, "/", http.StatusSeeOther)
}

然后在使用时调用它:

func PostHandler(w http.ResponseWriter, r *http.Request) {
    // 处理表单,然后:
    redirectBack(w, r)
}
}

  [1]: https://golang.org/pkg/net/http/#Request
  [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:

func PostHandler(w http.ResponseWriter, r *http.Request) {
	// Process form, then:
	if rf := r.Referer(); rf != "" {
		http.Redirect(w, r, rf, http.StatusSeeOther)
		return
	}

	// No Referer specified, supply your own response
	// or redirect to a default / home page
	http.Redirect(w, r, "/", http.StatusSeeOther)
}

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

func redirectBack(w http.ResponseWriter, r *http.Request) {
	if rf := r.Referer(); rf != "" {
		http.Redirect(w, r, rf, http.StatusSeeOther)
		return
	}

	// No Referer specified, supply your own response
	// or redirect to a default / home page
	http.Redirect(w, r, "/", http.StatusSeeOther)
}

And then using it:

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

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:

确定