CodeIgniter 始终运行此代码
我需要对每个请求运行一些代码,始终取决于用户是否登录。
我该把这段代码放在哪里?
我是否有可能传递数据,这段代码:
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
$this->load->model('users_model');
if ($this->tank_auth->is_logged_in())
{
$data = $this->users_model->get_userinfo($this->tank_auth->get_username());
if ($data['exp'] >= $data['max_exp']) {
$new_data = array(
'exp' => $data['exp'] - $data['max_exp'],
'level' => $data['level'] + 1,
);
$this->db->where('id', $data['id']);
$this->db->update('users', $new_data);
echo 'Hello?';
}
}
}
这是 MY_Controller,在 Core 类中。我可以进一步传递这些数据吗?我想,在真实的课堂上重新获取所有数据感觉是不必要的。
I need to run a few code on every request, always depending on If the user is logged in or not.
Where do I put this code?
Is there any possibility I can pass the data, This code:
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('security');
$this->load->library('tank_auth');
$this->lang->load('tank_auth');
$this->load->model('users_model');
if ($this->tank_auth->is_logged_in())
{
$data = $this->users_model->get_userinfo($this->tank_auth->get_username());
if ($data['exp'] >= $data['max_exp']) {
$new_data = array(
'exp' => $data['exp'] - $data['max_exp'],
'level' => $data['level'] + 1,
);
$this->db->where('id', $data['id']);
$this->db->update('users', $new_data);
echo 'Hello?';
}
}
}
This is MY_Controller, in the Core class.. Can I pass this data further? I guess, regrabbing all data, over again in the real class, feels unneccerary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 core 文件夹下创建一个类,然后让所有控制器扩展该类。
我做了一个与您所描述的非常相似的登录系统。
这是核心文件夹中的一个类:
注意:确保您的配置正确设置了继承前缀
然后控制器文件夹中的控制器将扩展 My_Controller
对于分层登录或更详细的示例,请参阅我的旧问题:
Codeigniter:通过继承控制登录权限
另外我的教程基于:
http://davidwinter。 me/articles/2009/02/21/authentication-with-codeigniter/
关于进一步传递数据的回答:
使用会话类?
http://codeigniter.com/user_guide/libraries/sessions.html
Create a class under the core folder then make all your controllers extend that class.
I did a log in system very similar to what you are describing.
This is a class in the core folder:
Note: make sure your config is correctly set up for inheritance prefix
Then your controllers in the controller folder will extend My_Controller
For tiered log-ins or a more detailed example see my old question:
Codeigniter: Controlling log in privileges with inheritance
Also the tutorial I based my stuff off of:
http://davidwinter.me/articles/2009/02/21/authentication-with-codeigniter/
Answer in regards to passing data further:
Use the session class?
http://codeigniter.com/user_guide/libraries/sessions.html