CodeIgniter 4 – Creating a “Hello World” application

This tutorial shows you how to create your first CodeIgniter 4 “Hello World” application.

Table of contents:

  1. CodeIgniter 4 Requirements
  2. Install CodeIgniter 4
  3. Create Hello World Controller
  4. Create Hello World View
  5. Run Your First Hello World Controller

1. CodeIgniter 4 Requirements

CodeIgniter 4 requires PHP 7.2 or higher and intl extension installed on your web server.

And the following PHP extensions are also recommended to be enabled on your server: php-json, php-mbstring, php-mysqlnd, php-xml.

It also currently has supported for the below database:

  • MySQL (5.1+) via the MySQLi driver
  • PostgreSQL via the Postgre driver
  • SQLite3 via the SQLite3 driver

 

2. Install CodeIgniter 4

CodeIgniter4 can be installed via Composer or Manually. Choose Manual installtion way if you would like to “install & go” install. Choose Composer installation if you plan to use 3rd party composer packages in your project or want to keep CodeIgniter up to date easily. We would like to recommend you to use Composer installation.

  • Manual installation

You can download and unzip the CodeIgniter 4 package on this page.

  • Composer installation (recommended)

Make sure that you have installed composer in your development machine. If you do not have it yet, you can install it by following the instructions on this page.

Then, use the following composer create-project command to install CodeIgniter 4.

composer create-project codeigniter4/appstarter MyFirstCodeIgniter4App


 

3. Create Hello World Controller

Create Helloworld.php file in app/Controllers/ directory and add the following content.

Controllers/Helloworld.php
<?php namespace App\Controllers;

class Helloworld extends BaseController
{
    public function index()
    {
        return view('Helloworld/index');
    }
}

In the index() method, we load Helloworld’s index view which we will create right below (in step 4).
 

4. Create HelloWorld View

In app/Views/ directory, let’s create Helloworld/index.php as below.

Views/Helloworld/index.php
<html>
<head>
    <title>My First CodeIgniter Application</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>

 

5. Run Your First Hello World Controller

First we need to start CodeIgniter Development server by running the following command in the root project:

php spark serve

Now visit your site using this URL.

http://localhost:8080/helloworld

If you did it right, you should see “Hello, World!” in your web browser.

Hello, World!

5 1 vote
Article Rating
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jayant kumar
jayant kumar
11 months ago

when i am writing php spark serve on command prompt it is giving message “‘php’ is not recognized as an internal or external command,
operable program or batch file.”

jayant kumar
jayant kumar
11 months ago

i am unable to run program on ci 4 , when i click on public folder it is running but what written in app folder routes file home controller is not running .Please give your mob no for discussion

3
0
Would love your thoughts, please comment.x
()
x