Set the default controller in CodeIgniter 3 for homepage

Set the default controller in CodeIgniter 3
Written by Gupta

In this tutorial, I am going to teach you how to set the default controller in CodeIgniter 3? When we access any website or project root URL then we get a page that displays on the browser. In the same way, we also set a default controller in the project which made in the CodeIgniter framework to open at the time of visiting the root URL because no anyone wants to open the sub-folder of URL.

When we develop a project then we set the homepage or login page as the default view. As we all know that a view page is accessing by the controller and then method name in CodeIgniter 3. For example, if our controller name is Test_controller and the method name is check_user() then we will access this function by http://localhost/test_controller/check_user or https://www.example.com/test_controller/check_user.

But think when you are developing a project and give it to the client then they don’t want to write controller name and method name in URL because it is an irritating task. Therefore, we need to set a controller as a default controller that will open as the first page of the project.

Let’s see how to set the default controller in CodeIgniter 3 for the homepage or login page?

Set the default controller in CodeIgniter 3 for homepage or Login page

CodeIgniter is a popular framework developed in PHP. It provides a config folder for every type of configuration in the project. Setting the default controller is also a part of the project configuration. This folder resides inside the application folder of the project.

First of all, open the routes.php file that resides inside the application/config folder.

Now, you are able to see a default_controller index at the bottom of the page. See code example:

$route['default_controller'] = 'Welcome';

In CodeIgniter 3, a default controller is a welcome controller. You can change the default controller name according to your requirement. For example, I have a controller with the name “Login_Controller” then I will set a default controller like this:

$route['default_controller'] = 'Login_Controller';

Now, you will be thinking that which method will access by default because the controller name and then method name should be in URL in CodeIgniter 3. Here, the index method will be accessed by default.

But, if you have a method with another name such as validate_credential() then you have to write method name also in the default controller. For example:

$route['default_controller'] = 'Login_Controller/validate_credential';

Sometimes, our controller resides inside a folder then we can set the default controller with their folder name like

$route['default_controller'] = 'folder_name/Login_Controller/validate_credential';

How to access the controller with a different name?

Sometimes, we need to access a controller with a different name. For example, if you have a controller with the name Homepage_Controller and you want to access this controller with a different name like dashboard then you have to create a route with an index dashboard. Let’s see

$route['dashboard'] = 'Homepage_Controller/first_page';

Now, you can access this controller by following URL: http://localhost/dashboard or https://www.example.com/dashboard

Conclusion and Final Words

Routes are the way to reach to the specific page. When a user triggers any specific route then they can reach the specific page related to that route. In MVC Framework, the route defines the path of the controller and method.

In this tutorial, we learned how to set the default controller in CodeIgniter 3? The default controller helps us to open a view page by default when anyone opens our root URL.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment

  • Thanks. I am 3 year experienced CodeIgniter developer but never know about get_where function. Thanks for sharing this article.