Set the default controller in CodeIgniter 3 with Sub-folder

By | February 20, 2021

 

Set the default controller in CodeIgniter 3 with Sub-folder. CodeIgniter 3 is a popular web framework of PHP that is used to develop a web-based project.

In today’s tutorial, we will learn How to set default controller in CodeIgniter framework? Before that we will know that What is the default controller and why is it set?

Set the default controller in CodeIgniter 3 – What is a default controller?

Default controller is called such controller which is open as well as root URL open. Whenever you are opening a website, a page is first shown to you in it, which is called default webpage.

If this webpage is to be set default in CodeIgniter then we set default controller because to access any page in CodeIgniter 3 we have to write the name of controller and method in the URL so that particular method of that controller can be accessed. |

Next we have to know about the route, so let’s know about the route a little bit. Whenever you write a URL in the address bar of the browser, that URL tells us the path of a particular webpage. Whenever that URL is triggered or hit, the request goes to the server and the server responds. This process is called routing through which data travels.

In MVC framework, the function of route is to tell the path of controller and method.

Steps to set default controller

CodeIgniter gives us one for each type of configuration config The folder provides different types of configuration files which are used for different tasks.

One within the same folder routes.php There is a file in which we define the route that means controller and method. Although in CodeIgniter 3 we can access a particular webpage by the name of the controller and method without defining the route, but when we have to access a webpage without typing the name of the controller and method, we define the route.

To set the default controller, first go to the root folder of your project. application folder and then config Go to the folder. Now you have a routes.php After opening the file, which will be some kind of code at the bottom.

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

CodeIgniter 3 has a welcome controller by default set, which you can change according to yourself. Like suppose we have Login_controller And one inside index () If the method is there, we will set the default controller as follows.

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

Now there is a question in the mind of many people that after all which method will be accessed by default, then I should tell that by here default index method is accessed here, if you write the above code in this way, it will work the same.

$route['default_controller'] = 'Login_controller/index';

When your controller is inside a sub-folder, you must also write the name of the sub-folder in the default controller. like

$route['default_controller'] = 'subfolder/Login_controller/index';

How to set multiple default controller?

You cannot set multiple default controllers, but you can create another route with another index. Like suppose you have a controller whose name Test_controller And one inside email_validation () The method is now if we access it with controller and method name, then it will take a very long URL to see, but you can shorten it by creating a route.

URL without route : http: // localhost / test_controller / email_valdiation

$route['email_valid'] = 'Test_controller/email_validation';

URL with route: http: // localhost / email_valid

Conclusion and Final Words

By setting the default controller, whenever someone opens the root URL of our website, it will show the default page or homepage. Anyone likes to write only the root URL to open the homepage, apart from that no one likes to write sub URL because it is difficult to remember and typing is a bit irritating task.

In this tutorial, we learned that How to set default controller in CodeIgniter 3? Stay connected with us to learn similar tutorials.

Read tutorial in English: Click here

read this also:

Leave a Reply

Your email address will not be published. Required fields are marked *