开发者

Codeigniter - should I access session data in a view?

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 开发者_如何学Cin. 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.


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:

$data['userdata'] = $this->session->all_userdata(); // returns and associative array

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.

$data['userdata'] = $_SESSION; // returns and associative array

or

$data['userdata'] = $this->session->userdata();

Documentation on userdata():

Gets the value for a specific $_SESSION item, or an array of all “userdata” items if not key was specified.

NOTE: This is a legacy method kept only for backwards compatibility with older applications. You should directly access $_SESSION instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜