如何在代码点火器中将变量从一个控制器传递给另一个控制器 [英] how to pass a variable from one controller to the other in Code igniter

查看:46
本文介绍了如何在代码点火器中将变量从一个控制器传递给另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Code Igniter。

I have just started learning Code Igniter .

我想知道如何将变量从一个控制器(first_cont.php)传递到另一个控制器(second_cont.php)?

I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?

任何帮助将不胜感激。

在此先感谢:)

推荐答案

这取决于具体情况。如果您想保留数据一段时间,那么会话数据将是您的理想之选。但是,如果只需要使用一次,则闪存数据可能更合适。

It will depend on the circumstances. If you want to retain the data for some time, then session data would be the way to go. However, if you only need to use it once, flash data might be more appropriate.

第一步是初始化会话库:

First step would be to initialise the session library:

$this->load->library('session');

然后将信息存储在闪存数据中:

Then store the information in flash data:

$this->session->set_flashdata('item', $myVar);

最后,在第二个控制器中,获取数据:

Finally, in the second controller, fetch the data:

$myVar = $this->session->flashdata('item');

很明显,这意味着您必须从第二个控制器重新初始化会话库,或者创建您自己的基本控制器来加载会话库,并使您的两个控制器都继承自该库。

Obviously this would mean you'd have to either initialise the session library again from the second controller, or create your own base controller that loads the session library and have both of your controllers inherit from that one.

这篇关于如何在代码点火器中将变量从一个控制器传递给另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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