Complete Concept of Named Route in CodeIgniter 4 Tutorial

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.

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.

.env

# CI_ENVIRONMENT = production// Do it toCI_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 –

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

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

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.

Read More[…]

Online Web Tutor is web development community found by Sanjay Kumar. It provides web development course for free worldwide by professionally trained instructors