Codeigniter - 我应该在视图中访问会话数据吗?
我是否应该从标题中为我的标题获取一些数据,该标题需要显示当前登录用户的一些详细信息。或者,在每个控制器中,加载用户数据,然后将其发送到相应的视图?似乎我应该从控制器中执行此操作,但将其放在标头中需要更少的代码。
Should I grab some data from a session variable for my header from the header which needs to display a few details for the user currently logged in. Or, in each controller, load user data then send it to the corresponding view? Seems like I should do it from the controllers but having it in header requires less code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该吗?为了可维护性和尊重 MVC 模式,我会说在控制器中执行此操作,我不认为一行代码会成为问题,您可以像这样获取所有内容:
然后将其传递给视图,并使用 $userdata['whatever'] 在视图中获取内容,这与从标头获取内容的代码量相同。
该函数位于此处
编辑 - 2015 年 11 月 3 日
版本 3.0
$this->session->all_userdata();
已被折旧。相反,直接访问$_SESSION
对象是首选方法,但是不带参数的$this->session->userdata();
可以与旧应用程序一起使用。或
有关 userdata() 的文档:
Should you? For the sake of maintainability and honoring the MVC pattern, I would say do it in the controller, I don't think one line of code is going to be an issue, you can get it all like this:
Then pass that to the view, and get the stuff out in the view with $userdata['whatever'] which is the same amount of code as getting it from the header anyway.
The function is located here
Edit - 03 November 2015
As of version 3.0
$this->session->all_userdata();
has been depreciated. Instead directly accessing the$_SESSION
object directly is the prefered method, however$this->session->userdata();
with no parameters could be used with older applications.or
Documentation on userdata():