📅  最后修改于: 2023-12-03 15:14:11.784000             🧑  作者: Mango
CodeIgniter-Flashdata is a library that provides a way to easily manage flashdata (temporary data that are kept for one page request) in CodeIgniter 3 applications.
Flashdata.php
file from the GitHub repository.application/libraries
directory.Code to load the library in your controller:
$this->load->library('flashdata');
Code to autoload the library:
$autoload['libraries'] = array('flashdata');
To set flashdata, use the set_flashdata()
method on the Flashdata library.
$this->flashdata->set_flashdata('key', 'value');
You can also set multiple flashdata using an array:
$flashdata = array(
'success' => 'Your account has been created!',
'error' => 'Unable to create account. Please try again later.'
);
$this->flashdata->set_flashdata($flashdata);
To get flashdata, use the flashdata()
method on the Flashdata library.
$data = $this->flashdata->flashdata('key');
If the flashdata does not exist, the method will return FALSE
.
You can also get all flashdata as an array:
$data = $this->flashdata->flashdata();
To delete flashdata, use the unset_flashdata()
method on the Flashdata library.
$this->flashdata->unset_flashdata('key');
You can also unset all flashdata:
$this->flashdata->unset_flashdata();
<?php
class Example extends CI_Controller {
public function index() {
$this->load->library('flashdata');
$success_message = 'Your account has been created!';
$error_message = 'Unable to create account. Please try again later.';
if ($this->create_account()) {
$this->flashdata->set_flashdata('success', $success_message);
} else {
$this->flashdata->set_flashdata('error', $error_message);
}
redirect('home');
}
private function create_account() {
// Code to create user account
return TRUE;
}
}
<?php
if ($this->flashdata->flashdata('success')) {
echo '<div class="success">' . $this->flashdata->flashdata('success') . '</div>';
} elseif ($this->flashdata->flashdata('error')) {
echo '<div class="error">' . $this->flashdata->flashdata('error') . '</div>';
}
?>
CodeIgniter-Flashdata is a simple and easy-to-use library for managing flashdata in your CodeIgniter 3 applications. It can help you simplify your code and make your applications more user-friendly by displaying temporary messages to your users.