diff --git a/HISTORY.md b/HISTORY.md index 9ef562a7..901071f6 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -499,68 +499,7 @@ However two more methods added to the `Controller`. - `RelTmpl() string`, returns the relative template directory based on the controller's name. These are useful when dealing with big `controllers`, they help you to keep align with any -future changes inside your application. - -Let's refactor our [ProfileController](_examples/mvc/controller-with-model-and-view/main.go) enhancemed by these two new functions. - -```go -func (pc *ProfileController) tmpl(relativeTmplPath string) { - // the relative template files directory of this controller. - views := pc.RelTmpl() - pc.Tmpl = views + relativeTmplPath -} - -func (pc *ProfileController) match(relativeRequestPath string) bool { - // the relative request path of this controller. - path := pc.RelPath() - return path == relativeRequestPath -} - -func (pc *ProfileController) Get() { - // requested: "/profile" - // so relative path is "/" because of the ProfileController. - if pc.match("/") { - - // views/profile/index.html - pc.tmpl("index.html") - return - } - - // requested: "/profile/browse" - // so relative path is "/browse". - if pc.match("/browse") { - pc.Path = "/profile" - return - } - - // requested: "/profile/me" - // so the relative path is "/me" - if pc.match("/me") { - - // views/profile/me.html - pc.tmpl("me.html") - return - } - - // requested: "/profile/$ID" - // so the relative path is "/$ID" - id, _ := pc.Params.GetInt64("id") - - user, found := pc.DB.GetUserByID(id) - if !found { - pc.Status = iris.StatusNotFound - - // views/profile/notfound.html - pc.tmpl("notfound.html") - pc.Data["ID"] = id - return - } - - // views/profile/profile.html - pc.tmpl("profile.html") - pc.User = user -} -``` +future changes inside your application. Want to learn more about these functions? Go to the [mvc/controller_test.go](mvc/controller_test.go) file and scroll to the bottom! @@ -769,7 +708,7 @@ and it adds its logic to its `BeginRequest`, [here](https://github.com/kataras/i Read access to the current route via the `Route` field. -**Using Iris MVC for code reuse** +**Using Iris MVC for code reuse** By creating components that are independent of one another, developers are able to reuse components quickly and easily in other applications. The same (or similar) view for one application can be refactored for another application with different data because the view is simply handling how the data is being displayed to the user. @@ -778,9 +717,7 @@ If you're new to back-end web development read about the MVC architectural patte Follow the examples below, -- [Hello world](_examples/mvc/hello-world/main.go) -- [Session Controller](_examples/mvc/session-controller/main.go) -- [A simple but featured Controller with model and views](_examples/mvc/controller-with-model-and-view). +https://github.com/kataras/iris/tree/master/_examples/#mvc ### Bugs diff --git a/README.md b/README.md index 47112136..d54c1c35 100644 --- a/README.md +++ b/README.md @@ -712,7 +712,7 @@ func (m Movie) IsValid() bool { ``` Iris is able to convert any custom data Structure into an HTTP Response Dispatcher, -so theoritically, something like the following is permitted if it's really necessary; +so theoretically, something like the following is permitted if it's really necessary; ```go // Dispatch completes the `kataras/iris/mvc#Result` interface. diff --git a/_examples/README.md b/_examples/README.md index b44c2f3e..b0058caa 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -183,15 +183,18 @@ func(c *ExampleController) Get() string | (string, string) | (string, int) | int | - (int, string | + (int, string) | (string, error) | + bool | + (any, bool) | + (bool, any) | error | (int, error) | (customStruct, error) | customStruct | (customStruct, int) | (customStruct, string) | - mvc.Result or (mvc.Result, error) + mvc.Result or (mvc.Result, error) and so on... ``` where [mvc.Result](https://github.com/kataras/iris/blob/master/mvc/method_result.go) is an interface which contains only that function: `Dispatch(ctx iris.Context)`. @@ -204,13 +207,20 @@ If you're new to back-end web development read about the MVC architectural patte Follow the examples below, +- [Hello world](mvc/hello-world/main.go) **UPDATED** +- [Session Controller](mvc/session-controller/main.go) **UPDATED** - [Overview - Plus Repository and Service layers](mvc/overview) **NEW** - [Login showcase - Plus Repository and Service layers](mvc/login) **NEW** - ### Subdomains diff --git a/_examples/mvc/controller-with-model-and-view/main.go b/_examples/mvc/controller-with-model-and-view/main.go deleted file mode 100644 index 61da2a35..00000000 --- a/_examples/mvc/controller-with-model-and-view/main.go +++ /dev/null @@ -1,111 +0,0 @@ -package main - -import ( - "sync" - - "github.com/kataras/iris" -) - -func main() { - app := iris.New() - app.RegisterView(iris.HTML("./views", ".html")) - - // 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.Run(iris.Addr(":8080")) -} - -// UserModel our example model which will render on the template. -type UserModel struct { - ID int64 - Username string -} - -// 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 { - iris.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 -} - -// These two functions are totally optional, of course, don't use them if you -// don't need such as a coupled behavior. -func (pc *ProfileController) tmpl(relativeTmplPath string) { - // the relative templates directory of this controller. - views := pc.RelTmpl() - pc.Tmpl = views + relativeTmplPath -} - -func (pc *ProfileController) match(relativeRequestPath string) bool { - // the relative request path based on this controller's name. - path := pc.RelPath() - return path == relativeRequestPath -} - -// Get method handles all "GET" HTTP Method requests of the controller's paths. -func (pc *ProfileController) Get() { // IMPORTANT - // requested: "/profile" - if pc.match("/") { - pc.tmpl("index.html") - return - } - - // requested: "/profile/browse" - // this exists only to proof the concept of changing the path: - // it will result to a redirection. - if pc.match("/browse") { - pc.Path = "/profile" - return - } - - // requested: "/profile/me" - if pc.match("/me") { - pc.tmpl("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("notfound.html") - pc.Data["ID"] = id - return - } - - pc.tmpl("profile.html") - pc.User = user -} diff --git a/_examples/mvc/controller-with-model-and-view/views/profile/index.html b/_examples/mvc/controller-with-model-and-view/views/profile/index.html deleted file mode 100644 index 7a97e7df..00000000 --- a/_examples/mvc/controller-with-model-and-view/views/profile/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - -
-- This is the main page of the /profile path, we'd use that to browser profiles. -
- - - \ No newline at end of file diff --git a/_examples/mvc/controller-with-model-and-view/views/profile/me.html b/_examples/mvc/controller-with-model-and-view/views/profile/me.html deleted file mode 100644 index f60f998c..00000000 --- a/_examples/mvc/controller-with-model-and-view/views/profile/me.html +++ /dev/null @@ -1,13 +0,0 @@ - - - -- This is the current's user imaginary profile space. -
- - - \ No newline at end of file diff --git a/_examples/mvc/controller-with-model-and-view/views/profile/notfound.html b/_examples/mvc/controller-with-model-and-view/views/profile/notfound.html deleted file mode 100644 index ca4b1986..00000000 --- a/_examples/mvc/controller-with-model-and-view/views/profile/notfound.html +++ /dev/null @@ -1,13 +0,0 @@ - - - -- User with {{.ID}} doesn't exist! -
- - - \ No newline at end of file diff --git a/_examples/mvc/controller-with-model-and-view/views/profile/profile.html b/_examples/mvc/controller-with-model-and-view/views/profile/profile.html deleted file mode 100644 index 3d16a91e..00000000 --- a/_examples/mvc/controller-with-model-and-view/views/profile/profile.html +++ /dev/null @@ -1,13 +0,0 @@ - - - -- This is the profile of a user with ID: {{.User.ID}} and Username: {{.User.Username}} -
- - - \ No newline at end of file diff --git a/_examples/mvc/hello-world/main.go b/_examples/mvc/hello-world/main.go index b83cb8ec..5d2c759a 100644 --- a/_examples/mvc/hello-world/main.go +++ b/_examples/mvc/hello-world/main.go @@ -55,32 +55,54 @@ func main() { type ExampleController struct { // if you build with go1.8 you have to use the mvc package always, // otherwise - // you can simply use `iris.Controller`. - mvc.Controller + // you can, optionally + // use the type alias `iris.C`, + // same for + // context.Context -> iris.Context, + // mvc.Result -> iris.Result, + // mvc.Response -> iris.Response, + // mvc.View -> iris.View + mvc.C } // Get serves // Method: GET // Resource: http://localhost:8080 -func (c *ExampleController) Get() { - c.ContentType = "text/html" - c.Text = "