CodeIgniter 4 – Create Your First Controller
In this post we will learn about Controller in CodeIgniter 4. The post will provide you code examples as well as clearly explanation.
Check the following post to learn more about CodeIgniter 4: Learn CodeIgniter 4 Tutorials.
What is Controller in CodeIgniter 4?
A Controller is simply a PHP class and contains public methods called action methods. It determines what response to send back to a user when a user makes a browser request.
For example:
http://your-domain.com/contact
When user makes the above request, CodeIgniter will attempt to find a Controller class named in Contact.php
in app/Controllers/
directory and load it and response result to user.
Controller Naming Convention in CodeIgniter 4
There is a bit of naming convention when creating a Controller in CodeIgniter 4 you need to keep in mind.
- Controller filename: The first char must be uppercase. The rest chars must be lowercased. For example:
- Correct controller filename:
Helloworld.php
,Login.php
,Forgotpasword.php
- Incorrect controller filename:
HelloWorld.php
,ForgotPassword.php
- Correct controller filename:
- Controller class name: Class name must be the same filename.
Create Your First Controller in CodeIgniter 4
Now it is time to create your first controller in CodeIgniter 4. For example, we will create a controller named “My First” Controller, let’s following the following steps:
- Create a PHP file named
Myfirst.php
and save it inapp/Controllers/
directory. - Open
Myfirst.php
and write the following content:app/Controllers/Myfirst.php<?php namespace App\Controllers; class Myfirst extends BaseController { public function index() { echo 'Hello! This is my first controller'; } }
Now visit your site using this URL:
http://localhost:8080/myfirst
, you will see theHello! This is my first controller
is displayed in your browser.
Bravo! you have created your first controller, let’s move forward.
As you can see in Myfirst
controller, we have created public method named index()
as action method. You may be wondering why it named index and can we use another name, show
for example? The answer is YES, we totally can use another action name.
In CodeIgniter, index
is default action, it means when we request to http://localhost:8080/myfirst
url, this url doesn’t have “action” specified, CodeIgniter then will load default index
action. Let’s see the below examples for more understanding:
Request URL | Loaded Controller/Action |
http://localhost:8080/myfirst | controller: Myfirst action: index |
http://localhost:8080/myfirst/show | controller: Myfirst action: show |
http://localhost:8080/myfirst/post | controller: Myfirst action: post |
Now let’s try to create another action named show
as following:
<?php
namespace App\Controllers;
class Myfirst extends BaseController
{
// ...
public function show()
{
echo 'This is show() action';
}
}
Now visit your site using this URL: http://localhost:8080/myfirst/show
, you will see the This is show() action
is displayed in your browser.
That’s all for your first controller. In next post, we will how you how to use View in CodeIgniter 4.
If you like this post or have any questions, feel free to add a comment on the below form. Thank you!