mirror of https://github.com/gin-gonic/gin.git
Add and fix the explanation of `HandleContext` (#1371)
Reference this issue #1323 1. There isn't any eg about `HandleContext` 2. The `c.Request.Path` of `HandleContext` Comment is not right Based on the above two points, I pull this request. If you think it's unnecessary, I will close this. Thx.
This commit is contained in:
parent
bf7803815b
commit
07cbe116a0
16
README.md
16
README.md
|
@ -1082,14 +1082,26 @@ Gin allow by default use only one html.Template. Check [a multitemplate render](
|
||||||
|
|
||||||
### Redirects
|
### Redirects
|
||||||
|
|
||||||
Issuing a HTTP redirect is easy:
|
Issuing a HTTP redirect is easy. Both internal and external locations are supported.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
r.GET("/test", func(c *gin.Context) {
|
r.GET("/test", func(c *gin.Context) {
|
||||||
c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
|
c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
Both internal and external locations are supported.
|
|
||||||
|
|
||||||
|
Issuing a Router redirect, use `HandleContext` like below.
|
||||||
|
|
||||||
|
``` go
|
||||||
|
r.GET("/test", func(c *gin.Context) {
|
||||||
|
c.Request.URL.Path = "/test2"
|
||||||
|
r.HandleContext(c)
|
||||||
|
})
|
||||||
|
r.GET("/test2", func(c *gin.Context) {
|
||||||
|
c.JSON(200, gin.H{"hello": "world"})
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Custom Middleware
|
### Custom Middleware
|
||||||
|
|
2
gin.go
2
gin.go
|
@ -329,7 +329,7 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleContext re-enter a context that has been rewritten.
|
// HandleContext re-enter a context that has been rewritten.
|
||||||
// This can be done by setting c.Request.Path to your new target.
|
// This can be done by setting c.Request.URL.Path to your new target.
|
||||||
// Disclaimer: You can loop yourself to death with this, use wisely.
|
// Disclaimer: You can loop yourself to death with this, use wisely.
|
||||||
func (engine *Engine) HandleContext(c *Context) {
|
func (engine *Engine) HandleContext(c *Context) {
|
||||||
c.reset()
|
c.reset()
|
||||||
|
|
Loading…
Reference in New Issue