Parametrized Routes in CodeIgniter 4 | Parameters Routing

Share this Article

This is very common to very web application i.e Passing parameters to URLs. Also called as parameterized routing of application.

Inside this article we will see the concept of Parameterized Routing in CodeIgniter 4. Already we have articles over Group routes in CodeIgniter 4 and Routing Tutorial in CodeIgniter 4.

We will cover these following topics –

  • URL Placeholders & With Parameters
  • URL with Query String
  • Optional parameter in URL

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.

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.


URL Placeholders & With Parameters

In CodeIgniter 4 application, there are several types of placeholder available to pass value to URL. Parameters as like of type Integer value, String value, Hashed value, Alpna numeric etc.

Let’s take an example to see How can we pass and access value to controller?

Open Routes.php file from /app/Config folder.

// .. Other routes

$routes->get("my-route/(:num)", "MyController::myRoute/$1");
$routes->get("sample-route/(:any)/(:num)", "MyController::sampleRoute/$1/$2");

Here, we have configured few routes.

  • First route will take a numeric value into URL as (:num) placeholder. We can pass any value there is no restriction with this placeholder i.e (:num) means only integer value. But I suggest, while developing any kind of application all things should be meaningful. That’s why use (:num) only when pass numeric value into URLs.
  • $1 in first route – Passing url value to myRoute() method.
  • Second route will take 2 values – one is any type means integer, string etc and other is for numeric value.
  • $1 is for (:any) value and $2 is for numeric value. These two values $1 & $2 we are passing inside sampleRoute() method.

Example URLs

  • /my-route/12
  • /my-route/45 … etc
  • /sample-route/hi/40
  • /sample-route/256/120

Access URL values to Controller

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class MyController extends BaseController
{
	public function myRoute($id)
	{
		// .. code here
	}

	public function sampleRoute($key, $id)
	{
		// .. code here
	}
}
  • myRoute($id) – Get (:num) value to method. This $id variable contains the URL value.
  • sampleRoute($key, $id) – Get (:any) & (:num) value. These two variables contains the URL value.

URL with Query String

For passing query string value, we don’t need to do anything with the route configuration.

Let’s add a simple route.

Open Routes.php file from /app/Config folder.

// .. Other routes

$routes->get("my-route", "MyController::myRoute");

Example URLs

  • /my-route?name=sanjay&role=auth
  • /my-route?message=success&error=0

Access URL values to Controller

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class MyController extends BaseController
{
	public function myRoute()
	{
		$all_query_values = $this->request->getVar();
        // It will return an array of all values
      
      	// get query variable - name
        $name = $this->request->getVar("name");
        // get query variable - role
        $role = $this->request->getVar("role");
	}
}
  • $this->request->getVar() – It returns all query variables with values into an array format.
  • $this->request->getVar(“name”); – It will return the single query variable value i.e name

Optional parameter in URL

In CodeIgniter 4, there is no such type documentation available which handles the situation for optional parameters.

But we can do this way to implement the concept of optional parameter into URLs.

Open Routes.php file from /app/Config folder.

// .. Other routes

$routes->get("my-route(:any)", "MyController::myRoute$1");

We need to use (:any) placeholder, keep in mind else any other placeholders like (:num), (:alpha) returns 404 Page not found error.

Example URLs

  • /my-route/some-value
  • /my-route

Access URL values to Controller

<?php

namespace App\Controllers;

use App\Controllers\BaseController;

class MyController extends BaseController
{
	public function myRoute($value = null)
	{
		// $value will be available if we pass into URL
        // Else no value in it.
        // Same route we can use for passing parameter or with no value.
	}
}

We hope this article helped you to learn Parametrized Routes in CodeIgniter 4 Tutorial in a very detailed way.

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