php 如何在codeigniter中使用重定向传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30962285/
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 data with redirect in codeigniter
提问by robins
In my controller i used this way. i want to pass a variable data to my index function of the controller through redirect
在我的控制器中,我使用了这种方式。我想通过重定向将变量数据传递给控制器的索引函数
$in=1;
redirect(base_url()."home/index/".$in);
and my index function is
我的索引函数是
function index($in)
{
if($in==1)
{
}
}
But I'm getting some errors like undefined variables.
How can i solve this?
但是我收到了一些错误,比如未定义的变量。
我该如何解决这个问题?
采纳答案by Arizona2014
So in the controller you can have in one function :
因此,在控制器中,您可以拥有一个功能:
$in=1;
redirect(base_url()."home/index/".$in);
And in the target function you can access the $in value like this :
在目标函数中,您可以像这样访问 $in 值:
$in = $this->uri->segment(3);
if(!is_numeric($in))
{
redirect();
}else{
if($in == 1){
}
}
I put segment(3) because on your example $in is after 2 dashes. But if you have for example this link structure : www.mydomain.com/subdomain/home/index/$in
you'll have to use segment(4).
我放了 segment(3) 因为在你的例子中 $in 是在 2 个破折号之后。但是如果你有这个链接结构:www.mydomain.com/subdomain/home/index/$in
你必须使用segment(4)。
Hope that helps.
希望有帮助。
回答by Rejoanul Alam
Use session to pass data while redirecting. There are a special method in CodeIgniter to do it called "set_flashdata"
重定向时使用会话传递数据。CodeIgniter 中有一个特殊的方法可以做到这一点,称为“set_flashdata”
$this->session->set_flashdata('in',1);
redirect("home/index");
Now you may get in
at index controller like
现在你可能会得到in
索引控制器
function index()
{
$in = $this->session->flashdata('in');
if($in==1)
{
}
}
Remember this data will available only for redirect and lost on next page request. If you need stable data then you can use URL with parameter & GET $this->input->get('param1')
请记住,此数据仅可用于重定向并在下一页请求时丢失。如果您需要稳定的数据,那么您可以使用带有参数和 GET 的 URL$this->input->get('param1')
回答by Eunil Gadiana
If you want to complicate things, here's how:
如果你想让事情复杂化,这里是如何:
On your routes.phpfile under application/config/routes.php, insert the code:
在application/config/routes.php下的routes.php文件中,插入代码:
$route['home/index/(:any)'] = 'My_Controller/index/';
Then on your controller [My_Controller], do:
然后在您的控制器[My_Controller] 上,执行以下操作:
function index($in){
if($in==1)
{
...
}
}
Finally, pass any value with redirect:
最后,通过重定向传递任何值:
$in=1;
redirect(base_url()."home/index/".$in);
Keep up the good work!
保持良好的工作!
回答by Faridul Khan
Use session to pass data while redirecting.There are two steps
重定向时使用session传递数据,分两步
Step 1 (Post Function):
步骤 1(后功能):
$id = $_POST['id'];
$this->session->set_flashdata('data_name', $id);
redirect('login/form', 'refresh');
Step2 (Redirect Function):
Step2(重定向功能):
$id_value = $this->session->flashdata('data_name');
回答by Marc Compte
More info would be very helpful, as this should be working.
更多信息将非常有帮助,因为这应该有效。
Things you can check:
您可以检查的事项:
- Is your controller named home.php? Going to
redirect(base_url()."home");
shows your home page? Make your index function public.
public function index($in) { .... }
- 你的控制器是 home.php 吗?要
redirect(base_url()."home");
显示您的主页? 公开您的索引函数。
public function index($in) { .... }