Codeigniter:查看访问会话的最佳做法 [英] Codeigniter: Best practice for View accessing session

查看:87
本文介绍了Codeigniter:查看访问会话的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我所读取的内容,视图应尽可能简单。

From what I've read, the View should be as simple as possible.

是否是访问视图中的会话变量的好习惯?

Is it good practice to access session variables in the view?

ie

// in the view
<?php if ($this->session->userdata('is_logged_in') : ?>
  // stuff

<?php endif; ?>


推荐答案

你的实际问题的直接答案是:是的,访问会话变量在视图内。因为会话或常规,他们正是一个变量。一个存储信息的地方。

The straight answer to your actual question is simply: Yes, it is fine to access session variables inside the view. Because session or regular, they are exactly that, a variable. A place to store information.

我经常使用 $ this-> session-> flashdata 用于显示标题内视图的已定义区域中的邮件。

I do this quite often with using the $this->session->flashdata for showing messages in a defined area of the view inside the header.

我说这个的原因是因为其他人似乎跳过你的实际问题,得到'为什么'你问的问题,哪里是最好的地方检查认证?Cadmus的答案是正确的头上我怎么处理这个,但是,再一次,不要认为你不应该访问会话数据从视图,但检查身份验证需要发生在控制器级别肯定。

The reason I say this is because the others seem to skip over your actual question to get at 'why' you asked the question, "where is the best place to check for auth?" for which Cadmus's answer is right on the head of how I handle this as well, but again, don't think you shouldn't access session "data" from the view, but checking for authentication needs to happen at the Controller level for sure.

这篇关于Codeigniter:查看访问会话的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆