在预控制器 codeigniter 挂钩中访问 CI 会话
我正在使用预控制器挂钩来从会话/cookie/浏览器语言检测中检测和设置站点语言..类似于这个答案: 存储
我还使用著名的 Tank Auth 库来管理用户,并且使用 codeigniter Session 类来 并在登录时管理用户会话。每个用户可以指定他想要的界面语言并将其存储在数据库中以供将来登录。每次登录时,语言信息都会从数据库复制到会话。
问题是:我无法从挂钩内部检查用户会话。我如何知道(在登录用户的情况下)他的会话是否具有语言信息?
我不确定在处理登录用户会话时挂钩方法是否是最好的方法。如果我能有更好的方法,请告诉我。
提前致谢
I am using a pre controller hook to detect and set the site language from the session/cookie/browser lang detection.. something like this answer:
Codeigniter language
I am also using the famous Tank Auth library to manage users and I am using the codeigniter Session class to store and manage the users sessions upon log in. Each user can specify the interface language he wants and store it in the database for future log ins. The lang info is copied from the DB to the session on every log in.
The problem is: I can't check the user session from inside the hook. How would I know (in case of logged in users) if his session has the lang info or not?
I am not sure if the hook approach is the best one in case of handling logged in users sessions. Let me know please if I can have a better approach.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 post_controller_constructor 飞节。它在控制器构造函数之后、控制器方法之前调用。它应该对你有用。对我来说确实如此
use a post_controller_constructor hock. It is called after the controller constructor and before the controller method. It should do the trick for you. For me it did
我通过使用 post_controller_constructor 钩子而不是 pre_controller 解决了这个问题。在该挂钩中,可以访问会话。因此,我在 CI 会话中设置检测到的语言,并且相应地加载语言文件。
我不确定这是否是最好的方法,但如果您不在任何控制器构造函数中使用 lang 库,它就可以正常工作。
I solved this by using a post_controller_constructor hook instead of pre_controller. In that hook, the session is accessible. So I am setting the detected lang in the CI session AND i am loading the language file accordingly.
I am not sure if this is the best approach, but it works fine if you are not using the lang library in any controller constructor.
一种可能的解决方案是尝试从预控制器挂钩在 CI 实例中加载会话库。
One possible solution is to try loading the session library in a CI instance from your pre-controller hook.