1
0
mirror of https://github.com/kataras/iris.git synced 2026-03-03 15:06:03 +00:00

Add context.FindClosest(n) to find closest paths - useful for 404 pages to suggest valid pages

Former-commit-id: 90ff7c9da5369df5bd99fbbecf9955a8c555fea5
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-12-16 02:00:42 +02:00
parent 4efe9004fa
commit a3f944b884
11 changed files with 137 additions and 30 deletions

View File

@@ -128,6 +128,7 @@ Navigate through examples for a better understanding.
- [Basic](routing/basic/main.go)
- [Controllers](mvc)
- [Custom HTTP Errors](routing/http-errors/main.go)
- [Not Found - Suggest Closest Paths](routing/not-found-suggests/main.go) **NEW**
- [Dynamic Path](routing/dynamic-path/main.go)
* [root level wildcard path](routing/dynamic-path/root-wildcard/main.go)
- [Write your own custom parameter types](routing/macros/main.go)

View File

@@ -54,7 +54,7 @@ func newApp() *iris.Application {
// Note that,
// Iris automatically adds a "tr" global template function as well,
// the only differene is the way you call it inside your templates and
// the only difference is the way you call it inside your templates and
// that it accepts a language code as its first argument: {{ tr "el-GR" "hi" "iris"}}
})
//
@@ -65,10 +65,18 @@ func newApp() *iris.Application {
func main() {
app := newApp()
// go to http://localhost:8080/el-gr/some-path (by path prefix)
// or http://el.mydomain.com8080/some-path (by subdomain - test locally with the hosts file)
// or http://localhost:8080/zh-CN/templates (by path prefix with uppercase)
// or http://localhost:8080/some-path?lang=el-GR (by url parameter)
// go to http://localhost:8080/el-gr/some-path
// ^ (by path prefix)
//
// or http://el.mydomain.com8080/some-path
// ^ (by subdomain - test locally with the hosts file)
//
// or http://localhost:8080/zh-CN/templates
// ^ (by path prefix with uppercase)
//
// or http://localhost:8080/some-path?lang=el-GR
// ^ (by url parameter)
//
// or http://localhost:8080 (default is en-US)
// or http://localhost:8080/?lang=zh-CN
//
@@ -77,6 +85,5 @@ func main() {
// or http://localhost:8080/other?lang=en-US
//
// or use cookies to set the language.
//
app.Run(iris.Addr(":8080"), iris.WithSitemap("http://localhost:8080"))
}

View File

@@ -0,0 +1,37 @@
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
app.OnErrorCode(iris.StatusNotFound, notFound)
// [register some routes...]
app.Get("/home", handler)
app.Get("/news", handler)
app.Get("/news/politics", handler)
app.Get("/user/profile", handler)
app.Get("/user", handler)
app.Get("/newspaper", handler)
app.Get("/user/{id}", handler)
app.Run(iris.Addr(":8080"))
}
func notFound(ctx iris.Context) {
suggestPaths := ctx.FindClosest(3)
if len(suggestPaths) == 0 {
ctx.WriteString("404 not found")
return
}
ctx.HTML("Did you mean?<ul>")
for _, s := range suggestPaths {
ctx.HTML(`<li><a href="%s">%s</a></li>`, s, s)
}
ctx.HTML("</ul>")
}
func handler(ctx iris.Context) {
ctx.Writef("Path: %s", ctx.Path())
}