Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MVC how to use multiple Handler? #754

Closed
NextGringo opened this issue Sep 18, 2017 · 9 comments
Closed

MVC how to use multiple Handler? #754

NextGringo opened this issue Sep 18, 2017 · 9 comments

Comments

@NextGringo
Copy link

Hi, I can't finde any info about how to use multiple handler for a function in a controller.

I would like to use for some routes(funcs) a pre handler func, that checks if a user is loggedIn and load then the needed Informations, else it should Skip the next handler and use the next after it.

Is there a possibility for that? Or need I code that all now in one handle?

@krokite
Copy link

krokite commented Sep 18, 2017

Yes, It is possible with Iris.

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

@NextGringo
Copy link
Author

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.

@praeto
Copy link

praeto commented Sep 19, 2017

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.

@NextGringo
Copy link
Author

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

@NextGringo
Copy link
Author

// 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

@praeto
Copy link

praeto commented Sep 19, 2017

@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")
		}
	})
}

@praeto
Copy link

praeto commented Sep 19, 2017

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

@NextGringo
Copy link
Author

ok, now it works.
Thanks.

@kataras
Copy link
Owner

kataras commented Oct 1, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants