mirror of
https://github.com/kataras/iris.git
synced 2025-01-24 11:11:03 +01:00
5e4b63acb2
# FAQ ### Looking for free support? http://support.iris-go.com https://kataras.rocket.chat/channel/iris ### Looking for previous versions? https://github.com/kataras/iris#version ### Should I upgrade my Iris? Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready. > Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes. **How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`. For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework). ### About our new home page http://iris-go.com Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome! [Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him. The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please! Read more at https://github.com/kataras/iris/blob/master/HISTORY.md Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
// Package main an example on how to naming your routes & use the custom 'url' HTML Template Engine, same for other template engines.
|
|
package main
|
|
|
|
import (
|
|
"github.com/kataras/iris"
|
|
"github.com/kataras/iris/context"
|
|
"github.com/kataras/iris/core/router"
|
|
"github.com/kataras/iris/view"
|
|
)
|
|
|
|
const (
|
|
host = "127.0.0.1:8080"
|
|
)
|
|
|
|
func main() {
|
|
app := iris.New()
|
|
|
|
// create a custom path reverser, iris let you define your own host and scheme
|
|
// which is useful when you have nginx or caddy in front of iris.
|
|
rv := router.NewRoutePathReverser(app, router.WithHost(host), router.WithScheme("http"))
|
|
// locate and define our templates as usual.
|
|
templates := view.HTML("./templates", ".html")
|
|
// add a custom func of "url" and pass the rv.URL as its template function body,
|
|
// so {{url "routename" "paramsOrSubdomainAsFirstArgument"}} will work inside our templates.
|
|
templates.AddFunc("url", rv.URL)
|
|
|
|
app.AttachView(templates)
|
|
|
|
// wildcard subdomain, will catch username1.... username2.... username3... username4.... username5...
|
|
// that our below links are providing via page.html's first argument which is the subdomain.
|
|
|
|
subdomain := app.Party("*.")
|
|
|
|
mypathRoute, _ := subdomain.Get("/mypath", emptyHandler)
|
|
mypathRoute.Name = "my-page1"
|
|
|
|
mypath2Route, _ := subdomain.Get("/mypath2/{paramfirst}/{paramsecond}", emptyHandler)
|
|
mypath2Route.Name = "my-page2"
|
|
|
|
mypath3Route, _ := subdomain.Get("/mypath3/{paramfirst}/statichere/{paramsecond}", emptyHandler)
|
|
mypath3Route.Name = "my-page3"
|
|
|
|
mypath4Route, _ := subdomain.Get("/mypath4/{paramfirst}/statichere/{paramsecond}/{otherparam}/{something:path}", emptyHandler)
|
|
mypath4Route.Name = "my-page4"
|
|
|
|
mypath5Route, _ := subdomain.Handle("GET", "/mypath5/{paramfirst}/statichere/{paramsecond}/{otherparam}/anything/{something:path}", emptyHandler)
|
|
mypath5Route.Name = "my-page5"
|
|
|
|
mypath6Route, err := subdomain.Get("/mypath6/{paramfirst}/{paramsecond}/staticParam/{paramThirdAfterStatic}", emptyHandler)
|
|
if err != nil { // catch any route problems when declare a route or on err := app.Run(...); err != nil { panic(err) }
|
|
panic(err)
|
|
}
|
|
mypath6Route.Name = "my-page6"
|
|
|
|
app.Get("/", func(ctx context.Context) {
|
|
// for username5./mypath6...
|
|
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
|
|
ctx.ViewData("ParamsAsArray", paramsAsArray)
|
|
if err := ctx.View("page.html"); err != nil {
|
|
panic(err)
|
|
}
|
|
})
|
|
|
|
// http://127.0.0.1:8080
|
|
app.Run(iris.Addr(host))
|
|
}
|
|
|
|
func emptyHandler(ctx context.Context) {
|
|
ctx.Writef("Hello from subdomain: %s , you're in path: %s", ctx.Subdomain(), ctx.Path())
|
|
}
|
|
|
|
// Note:
|
|
// If you got an empty string on {{ url }} or {{ urlpath }} it means that
|
|
// args length are not aligned with the route's parameters length
|
|
// or the route didn't found by the passed name.
|