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

How to use Session in none mvc project controller? #1057

Open
cfanboy opened this Issue Aug 7, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@cfanboy
Copy link

commented Aug 7, 2018

My project is APIs type, not MVC, I init the session in main function, but how can I use it in single controller methods?
I saw the session examples, all are using in the same main.go files and there is no any method in Context to get session instance like the previous version.
Thanks

@kataras

This comment has been minimized.

Copy link
Owner

commented Aug 7, 2018

How much experience with Go do you have? This is not an iris question... you can:

  • just delcare the variable outside of func main
  • create a service structure and have the sessions.Sessions instance there
  • pass the sessions.Sessions on the functions which create an iris handler
  • Iris can use dependency injection outside of MVC as well, take a look at hero package, like mvc : hero.Register(mySessions.Start) handler := hero.Handler(func(ctx iris.Context, session sessions.Session){{ /* use the session.Get/Set...*/}) <- magic? https://github.com/kataras/iris/tree/master/_examples/hero/basic

kataras added a commit that referenced this issue Aug 7, 2018

add example for hero *sessions.Session dependency which is used on an…
… index route outside of the main package, it is as easy as it shown. Added mostly after the issue: #1057 -- have fun
@kataras

This comment has been minimized.

Copy link
Owner

commented Aug 7, 2018

I just created an example for you @cfanboy although you can just read the mvc examples, the mvc it uses the hero under the hoods. Here you are: https://github.com/kataras/iris/tree/master/_examples/hero/sessions . Have fun!

@kataras kataras added the question label Aug 7, 2018

@cfanboy

This comment has been minimized.

Copy link
Author

commented Aug 7, 2018

Thank you !! It's very helpful for me

@kataras

This comment has been minimized.

Copy link
Owner

commented Aug 8, 2018

You are welcome! Don't hesitate to ask more!!

@kataras kataras pinned this issue Apr 14, 2019

@kataras kataras unpinned this issue Apr 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.