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

Update to 8.3.0 | MVC Models and Bindings and fix of #723 , read HISTORY.md

Former-commit-id: d8f66d8d370c583a288333df2a14c6ee2dc56466
This commit is contained in:
kataras
2017-08-18 17:09:18 +03:00
parent 398d1e816c
commit b96476d100
53 changed files with 12642 additions and 1046 deletions

View File

@@ -1,99 +1,130 @@
# Controllers from scratch
This example folder shows how I started to develop
the Controller idea inside the Iris web framework itself.
This folder shows how [@kataras](https://github.com/kataras) started to develop
the MVC idea inside the Iris web framework itself.
Now it's built'n feature and can be used as:
**Now** it has been enhanced and it's a **built'n** feature and can be used as:
```go
// +build go1.9
// file main.go
package main
import (
"github.com/kataras/iris/_examples/routing/mvc/persistence"
"sync"
"github.com/kataras/iris"
"github.com/kataras/iris"
"github.com/kataras/iris/mvc"
)
func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))
db := persistence.OpenDatabase("a fake db")
// when we have a path separated by spaces
// then the Controller is registered to all of them one by one.
//
// myDB is binded to the controller's `*DB` field: use only structs and pointers.
app.Controller("/profile /profile/browse /profile/{id:int} /profile/me",
new(ProfileController), myDB) // IMPORTANT
app.Controller("/user/{userid:int}", NewUserController(db))
// http://localhost:8080/
// http://localhost:8080/user/42
app.Run(iris.Addr(":8080"))
}
```
```go
// +build go1.9
// file user_controller.go
package main
import (
"time"
"github.com/kataras/iris/_examples/routing/mvc/persistence"
"github.com/kataras/iris"
)
// User is our user example controller.
type UserController struct {
iris.Controller
// All fields that are tagged with iris:"persistence"`
// are being persistence and kept between the different requests,
// meaning that these data will not be reset-ed on each new request,
// they will be the same for all requests.
CreatedAt time.Time `iris:"persistence"`
Title string `iris:"persistence"`
DB *persistence.Database `iris:"persistence"`
app.Run(iris.Addr(":8080"))
}
func NewUserController(db *persistence.Database) *User {
return &UserController{
CreatedAt: time.Now(),
Title: "User page",
DB: db,
}
// UserModel our example model which will render on the template.
type UserModel struct {
ID int64
Username string
}
// Get serves using the User controller when HTTP Method is "GET".
func (c *UserController) Get() {
c.Tmpl = "user/index.html"
c.Data["title"] = c.Title
c.Data["username"] = "kataras " + c.Params.Get("userid")
c.Data["connstring"] = c.DB.Connstring
c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
// DB is our example database.
type DB struct {
usersTable map[int64]UserModel
mu sync.RWMutex
}
// GetUserByID imaginary database lookup based on user id.
func (db *DB) GetUserByID(id int64) (u UserModel, found bool) {
db.mu.RLock()
u, found = db.usersTable[id]
db.mu.RUnlock()
return
}
var myDB = &DB{
usersTable: map[int64]UserModel{
1: {1, "kataras"},
2: {2, "makis"},
42: {42, "jdoe"},
},
}
// ProfileController our example user controller which controls
// the paths of "/profile" "/profile/{id:int}" and "/profile/me".
type ProfileController struct {
mvc.Controller // IMPORTANT
User UserModel `iris:"model"`
// we will bind it but you can also tag it with`iris:"persistence"`
// and init the controller with manual &PorifleController{DB: myDB}.
DB *DB
}
// Get method handles all "GET" HTTP Method requests of the controller's paths.
func (pc *ProfileController) Get() { // IMPORTANT
path := pc.Path
// requested: /profile path
if path == "/profile" {
pc.Tmpl = "profile/index.html"
return
}
// requested: /profile/browse
// this exists only to proof the concept of changing the path:
// it will result to a redirection.
if path == "/profile/browse" {
pc.Path = "/profile"
return
}
// requested: /profile/me path
if path == "/profile/me" {
pc.Tmpl = "profile/me.html"
return
}
// requested: /profile/$ID
id, _ := pc.Params.GetInt64("id")
user, found := pc.DB.GetUserByID(id)
if !found {
pc.Status = iris.StatusNotFound
pc.Tmpl = "profile/notfound.html"
pc.Data["ID"] = id
return
}
pc.Tmpl = "profile/profile.html"
pc.User = user
}
/* Can use more than one, the factory will make sure
that the correct http methods are being registered for this
controller, uncommend these if you want:
that the correct http methods are being registered for each route
for this controller, uncomment these if you want:
func (c *User) Post() {}
func (c *User) Put() {}
func (c *User) Delete() {}
func (c *User) Connect() {}
func (c *User) Head() {}
func (c *User) Patch() {}
func (c *User) Options() {}
func (c *User) Trace() {}
func (pc *ProfileController) Post() {}
func (pc *ProfileController) Put() {}
func (pc *ProfileController) Delete() {}
func (pc *ProfileController) Connect() {}
func (pc *ProfileController) Head() {}
func (pc *ProfileController) Patch() {}
func (pc *ProfileController) Options() {}
func (pc *ProfileController) Trace() {}
*/
/*
func (c *User) All() {}
func (c *ProfileController) All() {}
// OR
func (c *User) Any() {}
func (c *ProfileController) Any() {}
*/
```
Example can be found at: [_examples/routing/mvc](https://github.com/kataras/iris/tree/master/_examples/routing/mvc).
Example can be found at: [_examples/mvc](https://github.com/kataras/iris/tree/master/_examples/mvc).