在CodeIgniter中与重定向一起发送数据 [英] Sending data along with a redirect in CodeIgniter

查看:114
本文介绍了在CodeIgniter中与重定向一起发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的C(CRUD)函数,我想发送一个消息(错误或成功)与我的重定向从我插入的函数我写了。有一种方法可以通过重定向来粘贴POST字段吗?



在伪代码中,我有:

  function view_all {
//设置一些初始变量
$ this-> load-> view(viewing_page,$ data)
}

function insert {
if($ this-> db-> insert(my_table,$ _POST)){
$ message =All's well;
}
else {
$ message =whoops!;
}
redirect(view_all);
}

所以viewing_page理想上应该有

  if(isset($ message)){
echo $ message
}
pre>

所以在第一次通过,我没有看到任何消息,当/如果有一个插入,它弹出与消息相同的页面。谢谢!

解决方案

我相信 redirect header()。如果是这样,我不相信你可以发送数据与位置头。您可以使用会话vars或(不太好)在位置URL附加一个查询字符串来完成同样的事情。



对于一个接受的方式,在CodeIgniter请查看会话类文档页面的一半以下。


CodeIgniter支持flashdata或会话数据,只能用于下一个服务器请求,然后自动清除。这些可能非常有用,通常用于信息或状态消息(例如:记录2已删除)。


(现已删除 - 这里是存档版本在Flash消息上发布

:为了总结现在被删除的帖子,它显示了对消息进行urlencoding和附加为查询字符串(来自帖子的示例):

  header('Location:http://www.example.com/ index.php?message ='。urlencode($ message)); 

并使用两个框架(例如post)设置'flash'变量:

  // Zend Framework 
$ flashMessenger = $ this-> _helper-> FlashMessenger;
$ flashMessenger-> setNamespace('actionErrors');
$ flashMessenger-> addMessage($ message);

// CakePHP
$ this-> Session-> setFlash('您的帖子已保存。
$ this-> redirect('/ news / index');

当然,你可以使用 $ _ SESSION 直接(我的例子):

  //第一个请求
$ _SESSION ['flash'] = 这是一个简单的闪光信息。
//下一个请求
$ flash = $ _SESSION ['flash'];
unset($ _ SESSION ['flash']); // flash is one time only


I have a simple C (of CRUD) function, and I'd like to send a message (error or success) along with my redirect from the "insert" function I have written up. Is there a way to adhere a POST field with a redirect?

In pseudo code I have:

function view_all{
    //set up some initial variables
    $this->load->view(viewing_page, $data)
}

function insert{
    if ($this->db->insert(my_table, $_POST)){
        $message = "All's well";
    }
    else {
        $message = "whoops!";
    }
    redirect(view_all);
}

So the viewing_page ideally would have something like

if (isset($message)){
    echo $message
}

So on the first time through, I don't see any message, and when/if there's an insert, it pops up the same page with the message. Thanks!

解决方案

I believe redirect uses header(). If so, I don't believe you can send data along with a location header. You could accomplish the same thing using session vars or (not as good) appending a query string to the location URL.

For an 'accepted' way to do this in CodeIgniter look a little more than halfway down the session class documentation page.

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared. These can be very useful, and are typically used for informational or status messages (for example: "record 2 deleted").

This (now deleted - here's an archived version) post on flash messages covers both the query string and the session var method.

Update: To summarize the now deleted post, it showed both urlencoding a message and appending as a query string (example from post):

header('Location: http://www.example.com/index.php?message='.urlencode($message));

And setting a 'flash' variable using two frameworks (example from post):

//Zend Framework
$flashMessenger = $this->_helper->FlashMessenger;
$flashMessenger->setNamespace('actionErrors');
$flashMessenger->addMessage($message);

//CakePHP
$this->Session->setFlash('Your post has been saved.');
$this->redirect('/news/index');

Of course you can do roughly the same thing using $_SESSION directly (my example):

//first request
$_SESSION['flash'] = 'This is a simple flash message.';
//next request
$flash = $_SESSION['flash'];
unset($_SESSION['flash']); //flash is one time only

这篇关于在CodeIgniter中与重定向一起发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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