CodeIgniter 4 How To Redirect with Flashdata Example Tutorial

Reading Time: 4 minutes

Flash data, a transient session-based data storage mechanism, is invaluable for displaying messages or information across different HTTP requests in web applications.

In CodeIgniter 4, utilizing flash data in conjunction with redirection allows developers to persist data temporarily and display it on subsequent pages, enhancing user experiences. In this tutorial, we’ll see the process of redirecting with flash data in CodeIgniter 4.

Read More: CodeIgniter 4 How To Integrate Ckeditor Example Tutorial

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

Environment (.env) Setup

When we install CodeIgniter 4, we will have env file at root. To use the environment variables means using variables at global scope we need to do env to .env

Either we can do via renaming file as simple as that. Also we can do by terminal command.

Open project in terminal

cp env .env

Above command will create a copy of env file to .env file. Now we are ready to use environment variables.

Enable Development Mode

CodeIgniter starts up in production mode by default. You need to make it in development mode to see any error if you are working with application.

Open .env file from root.

# CI_ENVIRONMENT = production

 // Do it to 
CI_ENVIRONMENT = development

Now application is in development mode.

Process of Set & Get Flash Message

There are few simple steps which needs to follow to do this redirection in codeigniter with flash message.

Method #1

In this we will work with session and flash message.

Session init

$session = session();

Set Flash message

$session->setFlashdata('success', 'Data saved successfully!');

Read More: CodeIgniter 4 Drag and Drop Reorder Items with jQuery

Route Redirection

return redirect()->to('form');

form is a application route.


We need to get flash message into view and show it.

Display Flash message to view

<?php if (session()->getFlashdata('success')) { ?>
    <div class="alert alert-success">
        <?php echo session()->getFlashdata('success'); ?>
<?php } ?>

Let’s see the complete flow of this process.

Suppose we have a controller by which we submitted formdata.


namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Blog;

class BlogController extends BaseController
	public function index()
		return view('blog-form'); // view file of form

	public function submitData()
		$input = $this->request->getVar(); // get form data

		$blogObject = new Blog(); // model object init

		$data = [
			'title' => $input['title'],
			'author_name'    => $input['author_name'],
			'description' => $input['description']

		$blogObject->insert($data); // save data to database table

		$session = session(); // Session object init

		$session->setFlashdata('success', 'Data saved successfully!'); // set flash message

		return redirect()->to('form'); 

In view file i.e blog-form.php, get flash message and show.


<?php if (session()->getFlashdata('success')) { ?>
    <div class="alert alert-success">
        <?php echo session()->getFlashdata('success'); ?>
<?php } ?>


Alternative, Method #2

Here, we will redirect to route with flash message.

Attach Message with Redirection

return redirect()->to('form')->with('success', 'Data saved successfully!'); 

Display Flash Message


<?php if (session()->getFlashdata('success')) { ?>
    <div class="alert alert-success">
        <?php echo session()->getFlashdata('success'); ?>
<?php } ?>


That’s it.

We hope this article helped you to learn CodeIgniter 4 How To Redirect with Flashdata Example Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more