Documentation
Last updated
Last updated
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.
Iris has first-class support for the MVC (Model View Controller) architectural pattern, you'll not find these stuff anywhere else in the Go world. You will have to import the iris/mvc subpackage.
Iris web framework supports Request data, Models, Persistence Data and Binding with the fastest possible execution.
If you're new to back-end web development read about the MVC architectural pattern first, a good start is that wikipedia article.
Note: Read the Dependency Injection section before continue.
Characteristics
All HTTP Methods are supported, for example if want to serve GET
then the controller should have a function named Get()
, for POST
verb use the Post()
, for a parameter use the By
keyword, e.g. PostUserBy(id uint64)
which translates to POST: [...]/User/{id}.
Serve custom controller's struct's methods as handlers with custom paths(even with regex parametermized path) via the BeforeActivation
custom event callback, per-controller. Example:
Persistence data inside your Controller struct (share data between requests) by defining services to the Dependencies or have a Singleton
controller scope.
Share the dependencies between controllers or register them on a parent MVC Application, and ability to modify dependencies per-controller on the BeforeActivation
optional event callback inside a Controller, i.e func(c *MyController) BeforeActivation(b mvc.BeforeActivation) { b.Dependencies().Add/Remove(...) }
.
Access to the Context
as a controller's field(no manual binding is neede) i.e Ctx iris.Context
or via a method's input argument, i.e func(ctx iris.Context, otherArguments...)
.
Models inside your Controller struct (set-ed at the Method function and rendered by the View). You can return models from a controller's method or set a field in the request lifecycle and return that field to another method, in the same request lifecycle.
Flow as you used to, mvc application has its own Router
which is a type of iris/router.Party
, the standard iris api. Controllers
can be registered to any Party
, including Subdomains, the Party's begin and done handlers work as expected.
Optional BeginRequest(ctx)
function to perform any initialization before the method execution, useful to call middlewares or when many methods use the same collection of data.
Optional EndRequest(ctx)
function to perform any finalization after any method executed.
Inheritance, recursively, e.g. our mvc session-controller example, has the Session *sessions.Session
as struct field which is filled by the sessions manager.
Access to the dynamic path parameters via the controller's methods' input arguments, no binding is needed. When you use the Iris' default syntax to parse handlers from a controller, you need to suffix the methods with the By
word, uppercase is a new sub path. Example:
If mvc.New(app.Party("/user")).Handle(new(user.Controller))
func(*Controller) Get()
- GET:/user
.
func(*Controller) Post()
- POST:/user
.
func(*Controller) GetLogin()
- GET:/user/login
func(*Controller) PostLogin()
- POST:/user/login
func(*Controller) GetProfileFollowers()
- GET:/user/profile/followers
func(*Controller) PostProfileFollowers()
- POST:/user/profile/followers
func(*Controller) GetBy(id int64)
- GET:/user/{param:int64}
func(*Controller) PostBy(id int64)
- POST:/user/{param:int64}
If mvc.New(app.Party("/profile")).Handle(new(profile.Controller))
func(*Controller) GetBy(username string)
- GET:/profile/{param:string}
If mvc.New(app.Party("/assets")).Handle(new(file.Controller))
func(*Controller) GetByWildcard(path string)
- GET:/assets/{param:path}
Supported types for method functions receivers: int, int64, bool and string.
Optionally, response via output arguments, like we've shown at the Dependency Injection chapter. E.g.
Where mvc.Result
is just a type alias of hero.Result
:
This example is equivalent to a simple hello world application.
It seems that additional code you have to write doesn't worth it but remember that, this example does not make use of iris mvc features like the Model, Persistence or the View engine neither the Session, it's very simple for learning purposes, probably you'll never use such as simple controller anywhere in your app.
The cost we have on this example for using MVC on the "/hello" path which serves JSON is ~2MB per 20MB throughput on my personal laptop, it's tolerated for the majority of the applications but you can choose what suits you best with Iris, low-level handlers: performance or high-level controllers: easier to maintain and smaller codebase on large applications.
Read the comments carefully
Every exported
func prefixed with an HTTP Method(Get
, Post
, Put
, Delete
...) in a controller is callable as an HTTP endpoint. In the sample above, all funcs writes a string to the response. Note the comments preceding each method.
An HTTP endpoint is a targetable URL in the web application, such as http://localhost:8080/helloworld
, and combines the protocol used: HTTP, the network location of the web server (including the TCP port): localhost:8080
and the target URI /helloworld
.
The first comment states this is an HTTP GET method that is invoked by appending "/helloworld" to the base URL. The third comment specifies an HTTP GET method that is invoked by appending "/helloworld/welcome" to the URL.
Controller knows how to handle the "name" on GetBy
or the "name" and "numTimes" at GetWelcomeBy
, because of the By
keyword, and builds the dynamic route without boilerplate; the third comment specifies an HTTP GET dynamic method that is invoked by any URL that starts with "/helloworld/welcome" and followed by two more path parts, the first one can accept any value and the second can accept only numbers, i,e: "http://localhost:8080/helloworld/welcome/golang/32719", otherwise a 404 Not Found HTTP Error will be sent to the client instead.
The _examples/mvc and mvc/controller_test.go files explain each feature with simple paradigms, they show how you can take advandage of the Iris MVC Binder, Iris MVC Models and more...