CodeIgniter 4 – Creating a “Hello World” application
This tutorial shows you how to create your first CodeIgniter 4 “Hello World” application.
Table of contents:
- CodeIgniter 4 Requirements
- Install CodeIgniter 4
- Create Hello World Controller
- Create Hello World View
- Run Your First Hello World Controller
1. CodeIgniter 4 Requirements
CodeIgniter 4 requires PHP 7.3 or higher and intl
and mbstring
extension installed on your web server.
And the following PHP extensions are also recommended to be enabled on your server: php-json
, 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 installation 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.
<?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.
<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!
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.”
If you use XAMPP, then you will need to add this path “C:\xampp\php” to your PATH environment variable. Or if you use WAMP, the path will look like “C:\wamp64\bin\php\php7.2.21” — please check it in your machine.
Then it should work.
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