CodeIgniter 4 Redirection Complete Tutorial

Share this Article
Reading Time: 5 minutes
15,664 Views

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

Learn More –

Step by step we will see each cases in detail. 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.


.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.


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. Open Routes.php from /app/Config folder.

//...

# 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.

//...

<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>
               
//...

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.

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