How to configure route in CodeIgniter 4

Configuring the route in CodeIgniter 4 is useful for programming. By doing this, we have no need to write the full path, or we have no need to write the controller name in href tag, and also we can give a logical name that will be shown in URL instead of full controller name and also after configure routing, we have no need to show our function or method name which is working in our controller.

To do this, follow the following steps.
Here we will take an anchor tag for redirecting to another page.

Step 1 Create an anchor tag and set a name that you want for route. Following is an example.

<a class="btn btn-secondary" href="homepage">Home</a>
Step 2 Create a controller in app/controllers. Here we create a controller name MyController. You can create a controller with any name you want. And then create a function, here we create a function named MyFunction. And code here whatever you want to do. Here we just go to cpanel_view page.

Step 3 Create a view page app/views for redirect on click here we create a view page named cpanel_view.php
<?php namespace App\Controllers;
use CodeIgniter\Controller;

 use App\Models\SaveModel;
 use App\Models\TokenModel;

class MyController extends Controller{

public function MyFunction(){

 return view('cpanel_view');



Step 4 Now it’s time to set route. To do, go to App/Config/Route.php, and paste the below code.
$routes->add('/homepage', 'MyController ::MyFunction');
Explanation : In the above line ‘MyController’ is controller name MyFunction is the function name and ‘/homepage’ shows that, when we write ‘/homepage’ it will go to MyController and inside it. It goes inside MyFunction and execute it.

How to configure route in CodeIgniter 4expoundsofttechsolution How to configure route in CodeIgniter 4