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] using session info in middleinfo and parameters passing among handlers #1216

Closed
bluesky1024 opened this issue Mar 8, 2019 · 4 comments

Comments

@bluesky1024
Copy link

bluesky1024 commented Mar 8, 2019

background

I need to add some public operation before and after every request, which is depended on the session.

example

I Read the example about middleware giving as follows.
https://github.com/kataras/iris/tree/master/_examples/mvc/middleware.

func OverviewBeforeMiddleware(ctx iris.Context){
	fmt.Println("use")
	ctx.Next()
}
func OverviewDoneMiddleware(ctx iris.Context){
	fmt.Println("done")
	ctx.Next()
}
app.Use(middleware.OverviewBeforeMiddleware)
app.Done(middleware.OverviewDoneMiddleware)

question

  • the middleware handler can only accept the input iris.Context. Is there any possibility the public session be used in the middleware?
  • sometimes i want to pass the data handled in the middleware to the controllers' handlers or pass the data handled in the controllers' handlers to the DoneMiddleware. Maybe I can pass param with context, but I don't know if the context used in middleware and controllers' handler is the same one. Besides, is mvc in iris support to do it with other method?
@bluesky1024
Copy link
Author

supplement:
according to this issue as follows, the question to passing parameter among handlers is sovled.
#1180.

about the first question:
should i register the global *sessions.Sessions in the middleware package?

@kataras
Copy link
Owner

kataras commented Mar 8, 2019

Hello @bluesky1024 , sorry for the delay.

Yes, the question is already answered on the issue you posted with v11.2.
At your first question, you can register the same *sessions.Session in your controller as a field and bind it, we have the _examples/mvc/session-controller example.

@bluesky1024
Copy link
Author

hello @kataras, thanks for your reply and sorry for not replaying immediately.
As the example shows, the session is registerd in the controllers. Iris provide the function as follows for registering *sessions.session in UserController.

SessManager = sessions.New(sessions.Config{
	Cookie:  "my_session",
	Expires: 24 * time.Hour,
})
app.Register(SessManager.Start)
app.Handle(new(controllers.UserController))

However, i want to use the sessions in the middleware. Is middleware before the controller able to register the same session with some function in iris?

@kataras
Copy link
Owner

kataras commented Jul 23, 2019

You can call .Start many times, you can store the variables in the Context.Values().Set/Get, you can create a middleware which accepts a Session dependency using the hero dependency container, so yes you can pass it in middleware as well.

@kataras kataras closed this as completed Jul 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants