CodeIgniter 4 How To Redirect with Flashdata Example Tutorial

Share this Article
Reading Time: 4 minutes
347 Views

Inside this article we will see the concept i.e CodeIgniter 4 How To Redirect with Flashdata Example. Article contains the classified information about Flashdata in codeigniter 4 with redirect method.

If you are looking for a solution i.e How to Send and display flashdata message with redirect method in CodeIgniter 4 then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

CodeIgniter supports “flashdata”, or session data that will only be available for the next request, and is then automatically cleared. This can be very useful, especially for one-time informational, error or status messages.

Learn More –

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!');

Route Redirection

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

form is a application route.

Next,

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'); ?>
    </div>
<?php } ?>

Let’s see the complete flow of this process.

Suppose we have a controller by which we submitted formdata.

<?php

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'); ?>
    </div>
<?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'); ?>
    </div>
<?php } ?>

//...

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

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5