Comments (9)
BeginRequest
is a good abstraction but you can use an easier way, which is documented as well; You could just pass the handlers you want with app.Controller("/path", new(Controller), handler1, handler2, handler3)
those three handlers will be executed before "/path" controller.
from iris.
Yes, It is possible with Iris.
You need to Create and Use "Middleware" for that.
from iris.
Is there no direct or simpler method? Because I think the middelware is inappropriate for my purposes, but it wouldn't be nice to have to go back to the non-MVC-style for some paths.
from iris.
You can override BeginRequest for your mvc controller and add some handlers to the context.
Same for mvc route, if you need to execute a handler before your main code you can wrap it into a handler and use AddHandler.
from iris.
@praeto are you abel to show me a example, because i get with my try only a panic
from iris.
// BeginRequest saves login state to the context, the user id.
func (c *MyController) BeginRequest(ctx iris.Context) {
c.Ctx.AddHandler(func(ctx2 iris.Context) { // LINE 21
userID, err := ctx2.Params().GetInt64("user")
if err != nil {
logR.Println(err.Error())
}
if userID > 0 {
logR.Println(userID, "Erfolgreich")
}
})
}
./server
[WARN] 2017/09/19 10:11 Recovered from a route's Handler('MyProject/vendor/github.com/kataras/iris/mvc/activator.TController.HandlerOf.func1')
At Request: 200 /_external/payment/information/via/paypal GET 127.0.0.1
Trace: runtime error: invalid memory address or nil pointer dereference
/Go/src/runtime/asm_amd64.s:509
/Go/src/runtime/panic.go:491
/Go/src/runtime/panic.go:63
/Go/src/runtime/signal_unix.go:367
/GoProjects/src/MyProject/routes/MyController.go:21
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/mvc/activator/activator.go:136
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:824
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:1036
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/middleware/logger/logger.go:50
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/middleware/logger/logger.go:31
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:824
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:1036
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/middleware/recover/recover.go:56
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:837
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/context/context.go:979
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/core/router/handler.go:216
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/core/router/router.go:70
/GoProjects/src/MyProject/vendor/github.com/kataras/iris/core/router/router.go:147
/Go/src/net/http/server.go:2619
/Go/src/net/http/server.go:1801
/Go/src/runtime/asm_amd64.s:2337
from iris.
@NextGringo, you should first call default BeginRequest
for example:
// BeginRequest saves login state to the context, the user id.
func (c *MyController) BeginRequest(ctx iris.Context) {
c.Controller.BeginRequest(ctx)
c.Ctx.AddHandler(func(ctx2 iris.Context) { // LINE 21
userID, err := ctx2.Params().GetInt64("user")
if err != nil {
logR.Println(err.Error())
}
if userID > 0 {
logR.Println(userID, "Erfolgreich")
}
})
}
from iris.
@NextGringo, sorry, I forgot to mention that after adding handlers you should call them
func (c *MyController) BeginRequest(ctx iris.Context) {
c.Controller.BeginRequest(ctx) // <- call default BeginRequest
c.Ctx.AddHandler(...)
c.Ctx.Next() // <- call first handler
}
from iris.
ok, now it works.
Thanks.
from iris.
Related Issues (20)
- [BUG] template part Unable to load HOT 1
- [BUG] I used goroutine to affect the later HTTP before the service startup HOT 2
- [BUG] validation of binding is not effective
- [QUESTION] The session configuration provided by the iris framework is limited
- [FEATURE REQUEST] Get the data in the session through the session id
- [BUG] ctx.RemoveCookie need more special attribute(`domain` and `path`) value to remove cookies HOT 3
- slog is not in GOROOT HOT 1
- how to integrate SSE with mvc.
- [BUG] session recreation results in no session at all HOT 1
- How to integerate iris.FromStd with mvc.Application HOT 2
- [FEATURE REQUEST] SPA SubPath are not supported HOT 1
- ctx.Next() error
- Getting 403 Error on PUT | PATH | DELETE Request HOT 1
- [Question] Can not bind parameter of struct using ReadBody
- invalid version HOT 1
- [BUG]imported by a module that requires go 1.21
- [FEATURE REQUEST] How to configure swagger comments with iris using mvc
- How to use custom websockets[BUG]
- Is there an update guideline from 12.1.x to 12.2.x HOT 1
- [FEATURE REQUEST] Swagger generator and renderer HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from iris.