mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 10:41:03 +01:00
add the route state example in Routing
parent
b77bcaf407
commit
db83e4a4dc
65
Routing.md
65
Routing.md
|
@ -92,6 +92,69 @@ func handler(ctx iris.Context){
|
|||
}
|
||||
```
|
||||
|
||||
### Offline Routes
|
||||
|
||||
There is one special method in Iris that you can use too. It's called `None` and you can use it to hide a route from outsiders but still able to call it from other route's handlers through the `Context.Exec` method. Each API Handle method returns the Route value back. A Route contains two methods to retrieve the state: `IsOffline()` and `IsOnline()`. You can change the **state** of the route from **offline** to **online** and visa-versa through its `Route.Method` field's value. Of course each change of the router at serve-time requires an `app.RefreshRouter()` call which is safe to use. Take a look below a more complete example:
|
||||
|
||||
```go
|
||||
// file: main.go
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
invisibleRoute := app.None("/invisible/{username}", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello %s with method: %s", ctx.Params().Get("username"), ctx.Method())
|
||||
|
||||
if from := ctx.Values().GetString("from"); from != "" {
|
||||
ctx.Writef("\nI see that you're coming from %s", from)
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/change", func(ctx iris.Context) {
|
||||
|
||||
if invisibleRoute.IsOnline() {
|
||||
invisibleRoute.Method = iris.MethodNone
|
||||
} else {
|
||||
invisibleRoute.Method = iris.MethodGet
|
||||
}
|
||||
|
||||
// Refresh re-builds the router at serve-time
|
||||
// in order to be notified for its new routes.
|
||||
app.RefreshRouter()
|
||||
})
|
||||
|
||||
app.Get("/execute", func(ctx iris.Context) {
|
||||
// Same as navigating to "http://localhost:8080/invisible/iris"
|
||||
// when /change has being invoked and route state changed
|
||||
// from "offline" to "online"
|
||||
// Values and session can be shared when calling Exec from a "foreign" context.
|
||||
ctx.Values().Set("from", "/execute")
|
||||
|
||||
if invisibleRoute.IsOffline() {
|
||||
ctx.Exec("NONE", "/invisible/iris")
|
||||
}else{
|
||||
// or after "/change":
|
||||
ctx.Exec("GET", "/invisible/iris")
|
||||
}
|
||||
})
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
```
|
||||
|
||||
**How to run**
|
||||
|
||||
1. `go run main.go`
|
||||
2. Open a browser at `http://localhost:8080/invisible/iris` and you'll see that you get a `404 not found` error,
|
||||
3. however the `http://localhost:8080/execute` will be able to execute that route.
|
||||
4. Now, if you navigate to the `http://localhost:8080/change` and refresh the `/invisible/iris` tab you'll see that you can see it.
|
||||
|
||||
## Grouping Routes
|
||||
|
||||
A set of routes that are being groupped by path prefix can (optionally) share the same middleware handlers and template layout.
|
||||
|
@ -127,6 +190,8 @@ app.PartyFunc("/users", func(users iris.Party) {
|
|||
})
|
||||
```
|
||||
|
||||
## Path Parameters
|
||||
|
||||
Unlike other routers you'd seen, the Iris' one can handle various route paths without confliction between them.
|
||||
|
||||
Matches only GET "/".
|
||||
|
|
Loading…
Reference in New Issue
Block a user