php 如何在代码点火器中将变量从一个控制器传递到另一个控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12816552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to pass a variable from one controller to the other in Code igniter
提问by Aditi
I have just started learning Code Igniter .
我刚刚开始学习 Code Igniter 。
I want to know how can I pass a variable from one controller(first_cont.php) to other controller (second_cont.php) ?
我想知道如何将变量从一个控制器(first_cont.php)传递给另一个控制器(second_cont.php)?
Any help would be appreciated .
任何帮助,将不胜感激 。
Thanks in Advance :)
提前致谢 :)
回答by Matthew Daly
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.
显然,这意味着您必须从第二个控制器再次初始化会话库,或者创建您自己的基本控制器来加载会话库并使您的两个控制器都继承自该控制器。
回答by Kalpesh Patel
I think in codeigniter you can't pass variable, between two different controller. One obvious mechanism is to use session data.
我认为在 codeigniter 中你不能在两个不同的控制器之间传递变量。一种明显的机制是使用会话数据。
回答by Prasanth
Ok, here is something about MVC most will readily quote:
好的,这里是关于 MVC 的一些最容易引用的内容:
A Controller is for taking input, a model is for your logic, and, a view is for displaying.
控制器用于输入,模型用于逻辑,视图用于显示。
Now, strictly speaking you shouldn't want to send data from a controller to another. I can't think of any cases where that is required.
现在,严格来说,您不应该将数据从控制器发送到另一个控制器。我想不出任何需要这样做的情况。
But, if it is absolutely needed, then you could simply use redirectto just redirect to the other controller.
但是,如果绝对需要,那么您可以简单地使用redirect重定向到另一个控制器。
Something like:
就像是:
// some first_cont.php code here
redirect('/second_cont/valuereciever/value1')
// some second_cont.php code here
public function valureciever($value){
echo $value; // will output value1
}
回答by Vivek Pandey
In Codeigniter there are many way to pass the value from one controller to other.
在 Codeigniter 中,有很多方法可以将值从一个控制器传递到另一个控制器。
You can use codeigniter Session to pass the data from one controller to another controller.
您可以使用 codeigniter Session 将数据从一个控制器传递到另一个控制器。
For that you have to first include the library for session
为此,您必须首先包含会话库
$this->load->library('session');
Then You can set the flash data value using variable name.
然后您可以使用变量名称设置闪存数据值。
// Set flash data
$this->session->set_flashdata('variable_name', 'Value');
Them you can get the value where you want by using the codeigniter session flashdata
他们可以通过使用 codeigniter 会话 flashdata 获得所需的值
// Get flash data
$this->session->flashdata('variable_name');
Second Option codeigniter allow you to redirect the url from controll with controller name, method name and value and then you can get the value in another controller.
第二个选项 codeigniter 允许您使用控制器名称、方法名称和值从 controll 重定向 url,然后您可以在另一个控制器中获取该值。
// Passing the value
redirect('/another_controller_name/method_name/variable');
Then you can get the value in another controller
然后你可以在另一个控制器中获取值
public function method_name($variable)
{
echo $variable;
}
That all....
那一切....
回答by Mohammed Dawood
If you are using session in the first controller then dont unset that session in first controller, instead store the value which you want in the other controller like,
如果您在第一个控制器中使用会话,则不要在第一个控制器中取消设置该会话,而是将您想要的值存储在另一个控制器中,例如,
$sess_array = array('value_name1' => 'value1', 'value_name2' => 'value2');
$this->session->set_userdata('session_name', $sess_array);
then reload this session in the other controller as
然后在另一个控制器中重新加载这个会话
$session_data= $this->session->userdata('session_name');
$any_var_name = $session_data['value1'];
$any_var_name = $session_data['value2'];
this is how you can pass values from one controller to another....
这就是您如何将值从一个控制器传递到另一个控制器......

