mirror of
https://github.com/kataras/iris.git
synced 2025-01-23 02:31:04 +01:00
Page:
Routing override context
Pages
API versioning
Automatic public address
Benchmarks
Cache
Configuration
Content negotiation
Cookies
Documentation terms and conditions
Forms
Grpc
HTTP method override
HTTP referrer
Home
Host
Installation
Localization
MVC
Model validation
Our users
Project and community
Publications
Quick start
Request authentication
Response recorder
Routing context methods
Routing error handlers
Routing middleware
Routing override context
Routing path parameter types
Routing reverse lookups
Routing subdomains
Routing wrap the router
Routing
Sessions database
Sessions flash messages
Sessions
Sitemap
Starter kits
Support
Testing
URL query parameters
View
Websockets
dependency injection
6
Routing override context
Gerasimos (Makis) Maropoulos edited this page 2020-03-06 11:48:57 +02:00
In this section you will learn how to override the existing Context's methods.
The Context is an interface. However as you probably know, when using other frameworks you don't have that functionality of overriding even if it's used as an interface. With Iris you can attach your implementation to the context pool itself using the app.ContextPool.Attach
method.
- Let's get started by importing the
"github.com/kataras/iris/v12/context"
which is required here. - Secondly, create your own Context implementation.
- Add the
Do
andNext
methods where they just call thecontext.Do
andcontext.Next
package-level functions. - Use the Application's
ContextPool
to set it as the Context implementation that should be used for the route's handlers.
Example Code:
Please read the comments too.
package main
import (
"reflect"
"github.com/kataras/iris/v12"
// 1.
"github.com/kataras/iris/v12/context"
)
// 2.
// Create your own custom Context, put any fields you'll need.
type MyContext struct {
// Embed the `iris.Context` -
// It's totally optional but you will need this if you
// don't want to override all the context's methods!
iris.Context
}
// Optionally: validate MyContext implements iris.Context on compile-time.
var _ iris.Context = &MyContext{}
// 3.
func (ctx *MyContext) Do(handlers context.Handlers) {
context.Do(ctx, handlers)
}
// 3.
func (ctx *MyContext) Next() {
context.Next(ctx)
}
// [Override any context's method you want here...]
// Like the HTML below:
func (ctx *MyContext) HTML(format string, args ...interface{}) (int, error) {
ctx.Application().Logger().Infof("Executing .HTML function from MyContext")
ctx.ContentType("text/html")
return ctx.Writef(format, args...)
}
func main() {
app := iris.New()
// 4.
app.ContextPool.Attach(func() iris.Context {
return &MyContext{
// If you use the embedded Context,
// call the `context.NewContext` to create one:
Context: context.NewContext(app),
}
})
// Register a view engine on .html files inside the ./view/** directory.
app.RegisterView(iris.HTML("./view", ".html"))
// Register your route, as you normally do
app.Handle("GET", "/", recordWhichContextForExample,
func(ctx iris.Context) {
// use the context's overridden HTML method.
ctx.HTML("<h1> Hello from my custom context's HTML! </h1>")
})
// This will be executed by the
// MyContext.Context embedded default context
// when MyContext is not directly define the View function by itself.
app.Handle("GET", "/hi/{firstname:alphabetical}",recordWhichContextForExample,
func(ctx iris.Context) {
firstname := ctx.Values().GetString("firstname")
ctx.ViewData("firstname", firstname)
ctx.Gzip(true)
ctx.View("hi.html")
})
app.Listen(":8080")
}
// Should always print "($PATH) Handler is executing from 'MyContext'"
func recordWhichContextForExample(ctx iris.Context) {
ctx.Application().Logger().Infof("(%s) Handler is executing from: '%s'",
ctx.Path(), reflect.TypeOf(ctx).Elem().Name())
ctx.Next()
}
- What is Iris
- 📌Getting Started
- Host
- Configuration
- Routing
- HTTP Method Override
- API Versioning
- Content Negotiation
- Response Recorder
- HTTP Referrer
- Request Authentication
- URL Query Parameters
- Forms
- Model Validation
- Cache
- View
- Cookies
- Sessions
- Websockets
- Dependency Injection
- MVC
- gRPC
- Sitemap
- Localization
- Testing
- 🤓Resources
Home | Project | Quick Start | Technical Docs | Copyright © 2019-2020 Gerasimos Maropoulos. Documentation terms of use.