1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 10:27:06 +00:00

fix all _examples to the newest mvc, add comments to those examples and add a package-level .Configure in order to make it easier for new users. Add a deprecated panic if app.Controller is used with a small tutorial and future resource link so they can re-write their mvc app's definitions

Former-commit-id: bf07696041be9e3d178ce3c42ccec2df4bfdb2af
This commit is contained in:
Gerasimos (Makis) Maropoulos
2017-12-20 08:33:53 +02:00
parent fd0f3ed6cb
commit b78698f6c0
20 changed files with 432 additions and 283 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
func main() {
@@ -10,7 +11,7 @@ func main() {
templates := iris.HTML("./views", ".html").Layout("shared/layout.html")
app.RegisterView(templates)
app.Controller("/", new(Controller))
mvc.New(app).Register(new(Controller))
// http://localhost:9091
app.Run(iris.Addr(":9091"))
@@ -21,22 +22,28 @@ type Layout struct {
Title string
}
// Controller is our example controller.
// Controller is our example controller, request-scoped, each request has its own instance.
type Controller struct {
iris.Controller
Layout Layout `iris:"model"`
Layout Layout
}
// BeginRequest is the first method fires when client requests from this Controller's path.
// BeginRequest is the first method fired when client requests from this Controller's root path.
func (c *Controller) BeginRequest(ctx iris.Context) {
c.Controller.BeginRequest(ctx)
c.Layout.Title = "Home Page"
}
// EndRequest is the last method fired.
// It's here just to complete the BaseController
// in order to be tell iris to call the `BeginRequest` before the main method.
func (c *Controller) EndRequest(ctx iris.Context) {}
// Get handles GET http://localhost:9091
func (c *Controller) Get() {
c.Tmpl = "index.html"
c.Data["Message"] = "Welcome to my website!"
func (c *Controller) Get() mvc.View {
return mvc.View{
Name: "index.html",
Data: iris.Map{
"Layout": c.Layout,
"Message": "Welcome to my website!",
},
}
}

View File

@@ -2,13 +2,18 @@ package main
import (
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
func main() {
type postValue func(string) string
func main() {
app := iris.New()
app.Controller("/user", new(UserController))
mvc.New(app.Party("/user")).AddDependencies(
func(ctx iris.Context) postValue {
return ctx.PostValue
}).Register(new(UserController))
// GET http://localhost:9092/user
// GET http://localhost:9092/user/42
@@ -20,37 +25,44 @@ func main() {
}
// UserController is our user example controller.
type UserController struct {
iris.Controller
}
type UserController struct{}
// Get handles GET /user
func (c *UserController) Get() {
c.Ctx.Writef("Select all users")
func (c *UserController) Get() string {
return "Select all users"
}
// GetBy handles GET /user/42
func (c *UserController) GetBy(id int) {
c.Ctx.Writef("Select user by ID: %d", id)
// User is our test User model, nothing tremendous here.
type User struct{ ID int64 }
// GetBy handles GET /user/42, equal to .Get("/user/{id:long}")
func (c *UserController) GetBy(id int64) User {
// Select User by ID == $id.
return User{id}
}
// Post handles POST /user
func (c *UserController) Post() {
username := c.Ctx.PostValue("username")
c.Ctx.Writef("Create by user with username: %s", username)
func (c *UserController) Post(post postValue) string {
username := post("username")
return "Create by user with username: " + username
}
// PutBy handles PUT /user/42
func (c *UserController) PutBy(id int) {
c.Ctx.Writef("Update user by ID: %d", id)
func (c *UserController) PutBy(id int) string {
// Update user by ID == $id
return "User updated"
}
// DeleteBy handles DELETE /user/42
func (c *UserController) DeleteBy(id int) {
c.Ctx.Writef("Delete user by ID: %d", id)
func (c *UserController) DeleteBy(id int) bool {
// Delete user by ID == %id
//
// when boolean then true = iris.StatusOK, false = iris.StatusNotFound
return true
}
// GetFollowersBy handles GET /user/followers/42
func (c *UserController) GetFollowersBy(id int) {
c.Ctx.Writef("Select all followers by user ID: %d", id)
func (c *UserController) GetFollowersBy(id int) []User {
// Select all followers by user ID == $id
return []User{ /* ... */ }
}