1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 18:37:05 +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,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{ /* ... */ }
}