CodeIgniter 4 Redirection Complete Tutorial

Share this Article

Hi everyone, Inside this article we will cover each aspect of CodeIgniter 4 Redirection. There are several cases we can get while application development. Here, we have few cases mentioned as –

  • Redirection from one route to another route
  • Route Redirection with data
  • Named route Redirection.
  • Redirection with Flash Message, Inputs
  • Redirection Permanent 301

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Step by step we will see each cases in detail. Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed. Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


Settings Environment Variables

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

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.

CodeIgniter starts up in production mode by default. Let’s do it in development mode. So that while working if we get any error then error will show up.

# CI_ENVIRONMENT = production

// Do it to 

CI_ENVIRONMENT = development

Now application is in development mode.


What is Redirection ?

Redirection means transferring a URL to a different location(URL). For example we have a Add Form page. After submitting POST data to server, we need to redirect user to List page. So, from this concept we can do.

Redirection will be defined in controllers to point to locations.


CodeIgniter 4 Redirection in Depth

We are going to add few routes in /app/Config/Routes.php file.

//.. Other routes

# CASE #1
$routes->get("list-user", "User::listUser");
$routes->match(["get", "post"], "add-user", "User::addUser");

# CASE #2
$routes->get("list-user", "User::listUser", ["as" => "users"]); // Named route

Here, we have defined two routes. /list-user to list users whereas /add-user is for both get and post request type. It will be used for both rendering a layout and submitting form data.

Now, here what we want. When we submit data to /add-user via POST we need redirection to /list-user after saving data.

Basic Redirection

public function addUser()
{
  if ($this->request->getMethod() == "post") {

    // Save data here

    return redirect()->to(site_url("list-user"));
  }
}

Redirect With Named Route

To use named route, we have an option to use route().

public function addUser()
{
  if ($this->request->getMethod() == "post") {

    // Save data here

    return redirect()->route("users"); // Calling name defined in 'as' in Routes.php
  }
}

Redirect To Previous URL

Need to go to back of the route from where we came, Here we have –

// Go back to the previous page
return redirect()->back();

Redirection with Flash Message

If we want to go to location with some session flash value, So here we have the case.

public function addUser()
{
  if ($this->request->getMethod() == "post") {

    // Save data here

    return redirect()->route("users")->with("success", "Successfully, we are sending");
  }

  return view("add-user");
}

// Destination method of redirected URL
public function listUser()
{
    echo session()->get("name");
}

Redirection With Form Inputs

In Form submits, when we have errors then we loose old value from form inputs. So here, inside this case we are going to redirect with inputs.

<!-- View File -->
<form method="post" action="<?= site_url('add-user') ?>">

  <p>
      Name: <input type="text" name="name" value="<?= old('name') ?>">
  </p>
  <p>
      Email: <input type="text" name="email" value="<?= old('email') ?>">
  </p>

  <button>Submit</button>

</form>
               
<!-- Controller method -->
public function addUser()
{
   if ($this->request->getMethod() == "post") {

      // Save data here

       return redirect()->to("add-user")->withInput();
   }

   return view("add-user");
} 

Redirect Permanent

Route redirection from Routes.php file, we are going to redirect URLs automatically to a different URL like 301 redirection.

$routes->addRedirect('From Route', 'To Route');

We hope this article helped you to learn CodeIgniter 4 Redirection in Depth in a very detailed way. Also you can find CodeIgniter 4 Routing Tutorial here.

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.

Find More on CodeIgniter 4 here

Leave a Comment