Complete Concept of Named Route in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
973 Views

In CodeIgniter 4, we will see that routes configuration is totally different from it’s previous version. Inside this article we will learn about Named route in CodeIgniter 4.

Routes configuration are the application level configuration which controls the application redirection. Inside this we will cover complete concept of Named route in CodeIgniter 4. How to use Named routes additionally we will see into this.

Learn More –

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.


Turn Development Mode On

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 are Routes?

In any application Routes are the responsible section for responding the URL requests. In CodeIgniter 4 routes can be configured in Routes.php of /app/Config folder. When application requests any URLs, routes checked inside this file.

Example Route –

 $routes->get("/", "Home::index");

 $routes->get("/about-us", "Home::aboutUs");

Here, /about-us is URL route, Home is controller & aboutUs is a method from Home controller.


CodeIgniter 4 Named Route

When we create any route, we also have a option to add name to any route. Named route simply means a route which has a name, we can use it’s name to call it as simple as that to understand.

We can call that name to any linking at view file while creating routes.

$routes->get('/about-us', 'Home::aboutUs', ['as' => 'about']);

Here, /about-us is URL route, Home is controller, aboutUs is a method from Home controller and about is the name of the route.

To define name to any route, use ‘as’ keyword.

//...

$routes->add('our-products', 'Site::ourProducts', ['as' => 'products']);

$routes->get('our-services', 'Site::ourServices', ['as' => 'services']);

//...

How To Use Named Routes?

Usage of Named routes is pretty very simple. We can use it inside redirection or we can use it in view for linkings.

Usage in Redirection

In CodeIgniter 4, for redirection we use redirect() function.

//...

$routes->get('/sample-url', function(){
     // product is the name of "our-products" route. Look above.
	 return redirect('products'); 
});

//...

When we type /sample-url into URL, it will redirect to /our-products route. Here, we have used the concept of named route.

Usage in Route Linking

There are several ways to create anchor links in CodeIgniter 4. Here we will use route_to() function.

<a href="<?php echo route_to('products') ?>">Our Products</a>

<a href="<?php echo route_to('services') ?>">Our Services</a>

We can see we have used the names of routes instead the URL.

We hope this article helped you to learn Complete Concept of Named Route in CodeIgniter 4 Tutorial in a very detailed way.

Buy Me a Coffee

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