mvc

package
v8.4.3+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2017 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

type Controller struct {
	// Name contains the current controller's full name.
	//
	// doesn't change on different paths.
	Name string

	// request path and its parameters, read-write.
	// Path is the current request path, if changed then it redirects.
	Path string
	// Params are the request path's parameters, i.e
	// for route like "/user/{id}" and request to "/user/42"
	// it contains the "id" = 42.
	Params *context.RequestParams

	// some info read and write,
	// can be already set-ed by previous handlers as well.
	Status int
	Values *memstore.Store

	// view read and write,
	// can be already set-ed by previous handlers as well.
	Layout string
	Tmpl   string
	Data   map[string]interface{}

	ContentType string
	Text        string // response as string

	// give access to the request context itself.
	Ctx context.Context
	// contains filtered or unexported fields
}

Controller is the base controller for the high level controllers instances.

This base controller is used as an alternative way of building APIs, the controller can register all type of http methods.

Keep note that controllers are bit slow because of the reflection use however it's as fast as possible because it does preparation before the serve-time handler but still remains slower than the low-level handlers such as `Handle, Get, Post, Put, Delete, Connect, Head, Trace, Patch`.

All fields that are tagged with iris:"persistence"` or binded are being persistence and kept the same between the different requests.

An Example Controller can be:

type IndexController struct {
	Controller
}
func (c *IndexController) Get() {
	c.Tmpl = "index.html"
	c.Data["title"] = "Index page"
	c.Data["message"] = "Hello world!"
}

Usage: app.Controller("/", new(IndexController))

Another example with bind:

type UserController struct {
	mvc.Controller

	DB        *DB
	CreatedAt time.Time
}

// Get serves using the User controller when HTTP Method is "GET".

func (c *UserController) Get() {
	c.Tmpl = "user/index.html"
	c.Data["title"] = "User Page"
	c.Data["username"] = "kataras " + c.Params.Get("userid")
	c.Data["connstring"] = c.DB.Connstring
	c.Data["uptime"] = time.Now().Sub(c.CreatedAt).Seconds()
}

Usage: app.Controller("/user/{id:int}", new(UserController), db, time.Now()) Note: Binded values of context.Handler type are being recognised as middlewares by the router.

Look `core/router/APIBuilder#Controller` method too.

func (*Controller) BeginRequest

func (c *Controller) BeginRequest(ctx context.Context)

BeginRequest starts the main controller it initialize the Ctx and other fields.

It's called internally. End-Developer can ovverride it but it still MUST be called.

func (*Controller) EndRequest

func (c *Controller) EndRequest(ctx context.Context)

EndRequest is the final method which will be executed before response sent.

It checks for the fields and calls the necessary context's methods to modify the response to the client.

It's called internally. End-Developer can ovveride it but still should be called at the end.

func (*Controller) RelPath

func (c *Controller) RelPath() string

RelPath tries to return the controller's name without the "Controller" prefix, all lowercase prefixed with slash and splited by slash appended with the rest of the request path. For example: If UserController and request path is "/user/messages" then it's "/messages" if UserPostController and request path is "/user/post" then it's "/" if UserProfile and request path is "/user/profile/likes" then it's "/likes"

It's useful for things like path checking and redirect.

func (*Controller) RelTmpl

func (c *Controller) RelTmpl() string

RelTmpl tries to return the controller's name without the "Controller" prefix, all lowercase splited by slash and suffixed by slash. For example: If UserController then it's "user/" if UserPostController then it's "user/post/" if UserProfile then it's "user/profile/".

It's useful to locate templates if the controller and views path have aligned names.

func (*Controller) Route

func (c *Controller) Route() context.RouteReadOnly

Route returns the current request controller's context read-only access route.

func (*Controller) SetName

func (c *Controller) SetName(name string)

SetName sets the controller's full name. It's called internally.

func (*Controller) Write

func (c *Controller) Write(contents []byte) (int, error)

Write writes to the client via the context's ResponseWriter. Controller completes the `io.Writer` interface for the shake of ease.

type SessionController

type SessionController struct {
	Controller

	Manager *sessions.Sessions
	Session *sessions.Session
}

SessionController is a simple `Controller` implementation which requires a binded session manager in order to give direct access to the current client's session via its `Session` field.

func (*SessionController) BeginRequest

func (s *SessionController) BeginRequest(ctx context.Context)

BeginRequest calls the Controller's BeginRequest and tries to initialize the current user's Session.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL