Back to godoc.org
github.com/kataras/iris/v12/mvc

package mvc

v12.1.8
Latest Go to latest
Published: Feb 16, 2020 | License: BSD-3-Clause | Module: github.com/kataras/iris/v12

Index

Variables

var AutoBinding = hero.AutoBinding

AutoBinding used to be registered as dependency to try to automatically map and bind the inputs that are not already binded with a dependency.

A shortcut of `hero.AutoBinding`. Read more at: `hero#DefaultFallbackBinder`.

var HeroDependencies = true

HeroDependencies let you share bindable dependencies between package-level hero's registered dependencies and all MVC instances that comes later.

`hero.Register(...)` `myMVC := mvc.New(app.Party(...))` the "myMVC" registers the dependencies provided by the `hero.Register` func automatically.

Set it to false to disable that behavior, you have to use the `mvc#Register` even if you had register dependencies with the `hero` package.

Defaults to true.

var Try = hero.Try

Try is a type alias for the `hero#Try`, useful to return a result based on two cases: failure(including panics) and a succeess.

func NameOf

func NameOf(v interface{}) string

NameOf returns the package name + the struct type's name, it's used to take the full name of an Controller, the `ControllerActivator#Name`.

type AfterActivation

type AfterActivation interface {
	DependenciesReadOnly() ValuesReadOnly
	Singleton() bool
	// contains filtered or unexported methods
}

AfterActivation is being used as the only one input argument of a `func(c *Controller) AfterActivation(a mvc.AfterActivation) {}`.

It's being called after the `BeforeActivation`, and after controller's dependencies bind-ed to the fields or the input arguments but before server ran.

It's being used to customize a controller if needed inside the controller itself, it's called once per application.

type Application

type Application struct {
	Dependencies di.Values
	// Sorter is a `di.Sorter`, can be used to customize the order of controller's fields
	// and their available bindable values to set.
	// Sorting matters only when a field can accept more than one registered value.
	// Defaults to nil; order of registration matters when more than one field can accept the same value.
	Sorter      di.Sorter
	Router      router.Party
	Controllers []*ControllerActivator

	ErrorHandler di.ErrorHandler
	// contains filtered or unexported fields
}

Application is the high-level component of the "mvc" package. It's the API that you will be using to register controllers among with their dependencies that your controllers may expecting. It contains the Router(iris.Party) in order to be able to register template layout, middleware, done handlers as you used with the standard Iris APIBuilder.

The Engine is created by the `New` method and it's the dependencies holder and controllers factory.

See `mvc#New` for more.

func Configure

func Configure(party router.Party, configurators ...func(*Application)) *Application

Configure creates a new controller and configures it, this function simply calls the `New(party)` and its `.Configure(configurators...)`.

A call of `mvc.New(app.Party("/path").Configure(buildMyMVC)` is equal to

`mvc.Configure(app.Party("/path"), buildMyMVC)`.

Read more at `New() Application` and `Application#Configure` methods.

func New

func New(party router.Party) *Application

New returns a new mvc Application based on a "party". Application creates a new engine which is responsible for binding the dependencies and creating and activating the app's controller(s).

Example: `New(app.Party("/todo"))` or `New(app)` as it's the same as `New(app.Party("/"))`.

func (*Application) Clone

func (app *Application) Clone(party router.Party) *Application

Clone returns a new mvc Application which has the dependencies of the current mvc Application's `Dependencies` and its `ErrorHandler`.

Example: `.Clone(app.Party("/path")).Handle(new(TodoSubController))`.

func (*Application) Configure

func (app *Application) Configure(configurators ...func(*Application)) *Application

Configure can be used to pass one or more functions that accept this Application, use this to add dependencies and controller(s).

Example: `New(app.Party("/todo")).Configure(func(mvcApp *mvc.Application){...})`.

func (*Application) GetNamespaces

func (app *Application) GetNamespaces() websocket.Namespaces

GetNamespaces completes the websocket ConnHandler interface. It returns a collection of namespace and events that were registered through `HandleWebsocket` controllers.

func (*Application) Handle

func (app *Application) Handle(controller interface{}) *Application

Handle serves a controller for the current mvc application's Router. It accept any custom struct which its functions will be transformed to routes.

If "controller" has `BeforeActivation(b mvc.BeforeActivation)` or/and `AfterActivation(a mvc.AfterActivation)` then these will be called between the controller's `.activate`, use those when you want to modify the controller before or/and after the controller will be registered to the main Iris Application.

It returns this mvc Application.

Usage: `.Handle(new(TodoController))`.

Controller accepts a sub router and registers any custom struct as controller, if struct doesn't have any compatible methods neither are registered via `ControllerActivator`'s `Handle` method then the controller is not registered at all.

A Controller may have one or more methods that are wrapped to a handler and registered as routes before the server ran. The controller's method can accept any input argument that are previously binded via the dependencies or route's path accepts dynamic path parameters. The controller's fields are also bindable via the dependencies, either a static value (service) or a function (dynamically) which accepts a context and returns a single value (this type is being used to find the relative field or method's input argument).

func(c *ExampleController) Get() string | (string, string) | (string, int) | int | (int, string | (string, error) | bool | (any, bool) | error | (int, error) | (customStruct, error) | customStruct | (customStruct, int) | (customStruct, string) | Result or (Result, error) where Get is an HTTP Method func.

Examples at: https://github.com/kataras/iris/tree/master/_examples/mvc

func (*Application) HandleError

func (app *Application) HandleError(errorHandler func(ctx context.Context, err error)) *Application

HandleError registers a `hero.ErrorHandlerFunc` which will be fired when application's controllers' functions returns an non-nil error. Each controller can override it by implementing the `hero.ErrorHandler`.

func (*Application) HandleWebsocket

func (app *Application) HandleWebsocket(controller interface{}) *websocket.Struct

HandleWebsocket handles a websocket specific controller. Its exported methods are the events. If a "Namespace" field or method exists then namespace is set, otherwise empty namespace. Note that a websocket controller is registered and ran under a specific connection connected to a namespace and it cannot send HTTP responses on that state. However all static and dynamic dependency injection features are working, as expected, like any regular MVC Controller.

func (*Application) Party

func (app *Application) Party(relativePath string, middleware ...context.Handler) *Application

Party returns a new child mvc Application based on the current path + "relativePath". The new mvc Application has the same dependencies of the current mvc Application, until otherwise specified later manually.

The router's root path of this child will be the current mvc Application's root path + "relativePath".

func (*Application) Register

func (app *Application) Register(values ...interface{}) *Application

Register appends one or more values as dependencies. The value can be a single struct value-instance or a function which has one input and one output, the input should be an `iris.Context` and the output can be any type, that output type will be bind-ed to the controller's field, if matching or to the controller's methods, if matching.

These dependencies "values" can be changed per-controller as well, via controller's `BeforeActivation` and `AfterActivation` methods, look the `Handle` method for more.

It returns this Application.

Example: `.Register(loggerService{prefix: "dev"}, func(ctx iris.Context) User {...})`.

func (*Application) SortByNumMethods

func (app *Application) SortByNumMethods() *Application

SortByNumMethods is the same as `app.Sorter = di.SortByNumMethods` which prioritize fields and their available values (only if more than one) with the highest number of methods, this way an empty interface{} is getting the "thinnest" available value.

type BaseController

type BaseController interface {
	BeginRequest(context.Context)
	EndRequest(context.Context)
}

BaseController is the optional controller interface, if it's completed by the end controller then the BeginRequest and EndRequest are called between the controller's method responsible for the incoming request.

type BeforeActivation

type BeforeActivation interface {
	Dependencies() *di.Values
	// contains filtered or unexported methods
}

BeforeActivation is being used as the only one input argument of a `func(c *Controller) BeforeActivation(b mvc.BeforeActivation) {}`.

It's being called before the controller's dependencies binding to the fields or the input arguments but before server ran.

It's being used to customize a controller if needed inside the controller itself, it's called once per application.

type ControllerActivator

type ControllerActivator struct {

	// initRef BaseController // the BaseController as it's passed from the end-dev.
	Value reflect.Value // the BaseController's Value.
	Type  reflect.Type  // raw type of the BaseController (initRef).
	// contains filtered or unexported fields
}

ControllerActivator returns a new controller type info description. Its functionality can be overridden by the end-dev.

func (*ControllerActivator) Dependencies

func (c *ControllerActivator) Dependencies() *di.Values

Dependencies returns the write and read access of the dependencies that are came from the parent MVC Application, with this you can customize the dependencies per controller, used at the `BeforeActivation`.

func (*ControllerActivator) DependenciesReadOnly

func (c *ControllerActivator) DependenciesReadOnly() ValuesReadOnly

DependenciesReadOnly returns the read-only access type of the controller's dependencies. Used at `AfterActivation`.

func (*ControllerActivator) GetRoute

func (c *ControllerActivator) GetRoute(methodName string) *router.Route

GetRoute returns the first registered route based on the controller's method name. It can be used to change the route's name, which is useful for reverse routing inside views. Custom routes can be registered with `Handle`, which returns the *Route. This method exists mostly for the automatic method parsing based on the known patterns inside a controller.

A check for `nil` is necessary for unregistered methods.

See `GetRoutes` and `Handle` too.

func (*ControllerActivator) GetRoutes

func (c *ControllerActivator) GetRoutes(methodName string) []*router.Route

GetRoutes returns one or more registered route based on the controller's method name. It can be used to change the route's name, which is useful for reverse routing inside views. Custom routes can be registered with `Handle`, which returns the *Route. This method exists mostly for the automatic method parsing based on the known patterns inside a controller.

A check for `nil` is necessary for unregistered methods.

See `Handle` too.

func (*ControllerActivator) Handle

func (c *ControllerActivator) Handle(method, path, funcName string, middleware ...context.Handler) *router.Route

Handle registers a route based on a http method, the route's path and a function name that belongs to the controller, it accepts a forth, optionally, variadic parameter which is the before handlers.

Just like `Party#Handle`, it returns the `*router.Route`, if failed then it logs the errors and it returns nil, you can check the errors programmatically by the `Party#GetReporter`.

func (*ControllerActivator) HandleMany

func (c *ControllerActivator) HandleMany(method, path, funcName string, middleware ...context.Handler) []*router.Route

HandleMany like `Handle` but can register more than one path and HTTP method routes separated by whitespace on the same controller's method. Keep note that if the controller's method input arguments are path parameters dependencies they should match with each of the given paths.

Just like `Party#HandleMany`:, it returns the `[]*router.Routes`. Usage: func (*Controller) BeforeActivation(b mvc.BeforeActivation) {

b.HandleMany("GET", "/path /path1" /path2", "HandlePath")

}

func (*ControllerActivator) Name

func (c *ControllerActivator) Name() string

Name returns the full name of the controller, its package name + the type name. Can used at both `BeforeActivation` and `AfterActivation`.

func (*ControllerActivator) Router

func (c *ControllerActivator) Router() router.Party

Router is the standard Iris router's public API. With this you can register middleware, view layouts, subdomains, serve static files and even add custom standard iris handlers as normally.

This Router is the router instance that came from the parent MVC Application, it's the `app.Party(...)` argument.

Can used at both `BeforeActivation` and `AfterActivation`.

func (*ControllerActivator) Singleton

func (c *ControllerActivator) Singleton() bool

Singleton returns new if all incoming clients' requests have the same controller instance. This is done automatically by iris to reduce the creation of a new controller on each request, if the controller doesn't contain any unexported fields and all fields are services-like, static.

type Response

type Response = hero.Response

Response is a type alias for the `hero#Response`, useful for output controller's methods.

type Result

type Result = hero.Result

Result is a type alias for the `hero#Result`, useful for output controller's methods.

type ValuesReadOnly

type ValuesReadOnly interface {
	// Has returns true if a binder responsible to
	// bind and return a type of "typ" is already registered to this controller.
	Has(value interface{}) bool
	// Len returns the length of the values.
	Len() int
	// Clone returns a copy of the current values.
	Clone() di.Values
	// CloneWithFieldsOf will return a copy of the current values
	// plus the "s" struct's fields that are filled(non-zero) by the caller.
	CloneWithFieldsOf(s interface{}) di.Values
}

ValuesReadOnly returns the read-only access type of the controller's dependencies. Used at `AfterActivation`.

type View

type View = hero.View

View is a type alias for the `hero#View`, useful for output controller's methods.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier