mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 11:27:06 +00:00
Update to version 8.5.0 | NEW: MVC Output Result | Read HISTORY.md
Former-commit-id: 6a3579f2500fc715d7dc606478960946dcade61d
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// file: controllers/hello_controller.go
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/kataras/iris/mvc"
|
||||
)
|
||||
|
||||
// HelloController is our sample controller
|
||||
// it handles GET: /hello and GET: /hello/{name}
|
||||
type HelloController struct {
|
||||
mvc.C
|
||||
}
|
||||
|
||||
var helloView = mvc.View{
|
||||
Name: "hello/index.html",
|
||||
Data: map[string]interface{}{
|
||||
"Title": "Hello Page",
|
||||
"MyMessage": "Welcome to my awesome website",
|
||||
},
|
||||
}
|
||||
|
||||
// Get will return a predefined view with bind data.
|
||||
//
|
||||
// `mvc.Result` is just an interface with a `Dispatch` function.
|
||||
// `mvc.Response` and `mvc.View` are the built'n result type dispatchers
|
||||
// you can even create custom response dispatchers by
|
||||
// implementing the `github.com/kataras/iris/mvc#Result` interface.
|
||||
func (c *HelloController) Get() mvc.Result {
|
||||
return helloView
|
||||
}
|
||||
|
||||
// you can define a standard error in order to be re-usable anywhere in your app.
|
||||
var errBadName = errors.New("bad name")
|
||||
|
||||
// you can just return it as error or even better
|
||||
// wrap this error with an mvc.Response to make it an mvc.Result compatible type.
|
||||
var badName = mvc.Response{Err: errBadName, Code: 400}
|
||||
|
||||
// GetBy returns a "Hello {name}" response.
|
||||
// Demos:
|
||||
// curl -i http://localhost:8080/hello/iris
|
||||
// curl -i http://localhost:8080/hello/anything
|
||||
func (c *HelloController) GetBy(name string) mvc.Result {
|
||||
if name != "iris" {
|
||||
return badName
|
||||
// or
|
||||
// GetBy(name string) (mvc.Result, error) {
|
||||
// return nil, errBadName
|
||||
// }
|
||||
}
|
||||
|
||||
// return mvc.Response{Text: "Hello " + name} OR:
|
||||
return mvc.View{
|
||||
Name: "hello/name.html",
|
||||
Data: name,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user