index.html
and Welcome.php
are the two inbuilt files that comes with the CodeIgniter.
Now, create a new file "splessons.php" in the same folder and enter the below code:
[php]
<?php
class Splessons extends CI_Controller {
public function index() {
echo "Hello! Welcome to SPLessons";
}
}
?>
[/php]
Output:
[php]Hello! Welcome to SPLessons[/php]
In the above example, controller Splessons is created that extends CI_Controller which is a in-built class of CodeIgniter and has to be extended when own classes are created. The name of the controller should start with the capital letter.
The "Splessons" controller in the above example will display an output when called by the below URI in the browser :
[html]http://localhost/Codeigniter/index.php/splessons[/html]
In the above URI, "splessons" is the class name of CodeIgniter Controller or the name of controller. But in URI, the controller should be of lower case. The syntax for calling a controller will be as follows.
[html]http://www.your-domain.com/index.php/controller/method-name[/html] This is default function
" which is nothing but "index" method output which appears automatically as it is the default method of CodeIgniter.
(ii) index.php/splessons/index
[html]
http://www.your-domain.com/index.php/splessons/index
[/html]
This URI is similar to the case 1 and the output will be "This is default function
" which is nothing but "index" method output that appears after entering the method name manually.
(iii) index.php/splessons/index/splessons/hi
[html]
http://www.your-domain.com/index.php/splessons/hi
[/html]
When above URI is entered the output will be "This is hi function
" which is nothing but "hi" method output that is present in the Splessons controller as the user's own method.
__construct()
is used.
[php]
<?php
class Splessons extends CI_Controller {
public function __construct(){
parent::__construct();
echo "This is initialization method<br>";
}
public function index() {
echo "This is default function.";
}
public function hi() {
echo "This is hi function.";
}
}
?>
[/php]
The URI's can be any of the three mentioned below.
[html]
http://www.your-domain.com/index.php/splessons
http://www.your-domain.com/index.php/splessons/index
http://www.your-domain.com/index.php/splessons/hi
[/html]