Coder Social home page Coder Social logo

MVC how to use multiple Handler? about iris HOT 9 CLOSED

kataras avatar kataras commented on May 8, 2024
MVC how to use multiple Handler?

from iris.

Comments (9)

kataras avatar kataras commented on May 8, 2024 1

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.

krokite avatar krokite commented on May 8, 2024

Yes, It is possible with Iris.

You need to Create and Use "Middleware" for that.

from iris.

NextGringo avatar NextGringo commented on May 8, 2024

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.

praeto avatar praeto commented on May 8, 2024

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.

NextGringo avatar NextGringo commented on May 8, 2024

@praeto are you abel to show me a example, because i get with my try only a panic

from iris.

NextGringo avatar NextGringo commented on May 8, 2024
// 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.

praeto avatar praeto commented on May 8, 2024

@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.

praeto avatar praeto commented on May 8, 2024

@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.

NextGringo avatar NextGringo commented on May 8, 2024

ok, now it works.
Thanks.

from iris.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.