Complete Concept of Named Route in CodeIgniter 4 Tutorial

Reading Time: 4 minutes

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 –

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.

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.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more