Starting from a fresh installation of Codeigniter 3, here is a simple way to start with an Hello World application, to break the ice with this solid PHP framework.
To do this you can start creating the view that we want to be shown for our Hello World app.
We are going to put it in your application folder, here:
In hello_world.php
(/application/views/
)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
It's just a simple HTML content.
Now, in order to make this view shown, we need a controller. The controller is the one that will recall the view in order for its content to be displayed.
In order for it to work properly, the controller needs to go in the proper controllers folder.
Here is where we are going to place our Hello World controller:
/application/controllers/Hello_world.php
(The controller's name is generally snake_case with the first letter uppercase)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello_world extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index(){
$this->load->view('hello_world');
}
}
The default function for a controller is the index function.
Now you will be able to see the content of your Hello World page accessing the following address:
http://[your_domain_name]/index.php/hello_world
or, in case you applied the fix using .htaccess (go back to the installation page for the fix)
http://[your_domain_name]/hello_world
(If you are working locally, most likely the address where you'll find your page is: http://localhost/hello_world
)
The URL is actually formed calling your controller class (in this case Hello_world
, but using all lowercase in the URL). In this case it is enough, since we used the index function. If we would have used a different function name (let's say greetings
), we should have used an URL like this:
http://[your_domain_name]/hello_world/greetings
Which is structured as /[controller_name]/[method_name]
.
Here you go! Your first Codeigniter application is working!