CodeIgniter闪存数据


CodeIgniter Flash Data

我在CodeIgniter中处理Flash数据。

我基本上想:

将类别添加到数据库将用户重定向回页面显示成功弹出消息"您的类别已创建"

到目前为止,我可以成功地将类别添加到数据库中,并且用户输入得到了正确验证,唯一的问题是我不知道如何创建弹出成功消息。(我不想加载成功视图),只需重定向回它们的来源,并在上角显示小消息或其他内容。

闪存数据是正确的方法吗?

在控制器中:

//add to db
// load session library if not auto-loaded
$this->session->set_flashdata('msg', 'Category added');
redirect('controller/method');

视图中:

<script>
// assumes you're using jQuery
$(document).ready(function() {
$('.confirm-div').hide();
<?php if($this->session->flashdata('msg')){ ?>
$('.confirm-div').html('<?php echo $this->session->flashdata('msg'); ?>').show();
<?php } ?>
});
</script>

您可以执行不同的会话消息,这取决于您从控制器传递给视图的内容。注意到我正在使用Bootstrap作为我的CSS主干。

在视图中,

对于成功案例,

<?php if ($this->session->flashdata('category_success')) { ?>
        <div class="alert alert-success"> <?= $this->session->flashdata('category_success') ?> </div>
    <?php } ?>

对于错误情况,

<?php if ($this->session->flashdata('category_error')) { ?>
    <div class="alert alert-danger"> <?= $this->session->flashdata('category_error') ?> </div>
<?php } ?>

在控制器中,

对于成功案例,

$this->session->set_flashdata('category_success', 'Success message.');
redirect("To your view");

对于错误情况,

$this->session->set_flashdata('category_error', 'Error message.');
redirect("To your view");

如需更多参考,您可以访问:http://www.codeigniter.com/userguide2/libraries/sessions.html

使用三元运算符:

设置闪存数据:

$this->session->set_flashdata('insertproduct', 'Product added successfully');
$this->session->set_flashdata('deleteproduct','Delete added successfully');

使用Flash会话数据:

<?php if($this->session->flashdata('insertproduct')):echo $this->session->flashdata('insert');endif; ?><br/>
<?php if($this->session->flashdata('delete')): echo $this->session->flashdata('delete'); endif;?>

你可以试试这个-

控制器:

    $this->session->set_flashdata('success', 'Success Message...');
    OR
    $this->session->set_flashdata('error', 'Error Message...');
    OR
    $this->session->set_flashdata('warning', 'Warning Message...');
    OR
    $this->session->set_flashdata('info', 'Info Message...');

视图:

    <?php if($this->session->flashdata('success')){ ?>
        <div class="alert alert-success">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Success!</strong> <?php echo $this->session->flashdata('success'); ?>
        </div>
    <?php } else if($this->session->flashdata('error')){  ?>
        <div class="alert alert-danger">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Error!</strong> <?php echo $this->session->flashdata('error'); ?>
        </div>
    <?php } else if($this->session->flashdata('warning')){  ?>
        <div class="alert alert-warning">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Warning!</strong> <?php echo $this->session->flashdata('warning'); ?>
        </div>
    <?php } else if($this->session->flashdata('info')){  ?>
        <div class="alert alert-info">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Info!</strong> <?php echo $this->session->flashdata('info'); ?>
        </div>
    <?php } ?>

是的,只需检查闪存数据是否可用,如果可用,则显示消息,如果不可用,则不显示。就这么简单。

p.s.您应该始终在POST请求之后进行重定向。

CodeIgniter的Flash数据使用PHP session变量。它在会话名称中放置一个:old,以便它只持续一个数据库调用。它的功能和目的是做你想做的事情,所以,是的,这是一种很好的方式来处理这些类型的事情。

请记住,如果您要使用此,则必须包括$this->session->library('session')

如果你不确定如何实际使用flash_data,我建议你阅读我之前链接的文档。

$this->session->set_flashdata(
    'category_success', 
    'Your category has been created'
);
redirect(); //location
echo $this->session->flashdata('category_success');
//Set Flash messages
$this->session->set_flashdata('post_created', 'Your post has been Posted!');
redirect('Posts/index');
//In Posts View you will have
<?php if($this->session->flashdata('post_created')) : ?>
    <?php echo '<p class="alert alert-success"> ' .$this->session->flashdata('post_created'). '</p>'; ?>
<?php endif; ?>