Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Sunday 2 February 2014

Hello World Program in CodeIgniter

Hello friends, here is another post about CodeIgniter first program. Before we learn how to create Hello World program in CodeIgniter, first we have to understand what is Controller?

1. What is Controller?

Controller is a class that works as intermediary between Model and View. In CodeIgniter, Controller runs first then Controller calls/loads Models or Views as required.
CodeIgniter Hello World Program - Basic Controller Program
Hello World Program In CodeIgniter
 
2. Simple Hello World Program (Controller)


Create PHP file named first.php and write following code in it.

<?php
class First extends CI_Controller
{
 public function __construct()
 {
  parent::__construct();
 }
 public function index()
 {
  echo "Hello World!";
 }
}
?>
After creating first.php file then save it in the folder: application/controller/

In our example, there are two functions one is constructor and another function name is index(). index() function is a special kind of function that is called automatically when you not specify function name in address bar, after the controller name. In our above example, we write code inside the index() function and at the time of run we have not specify method name, at that time index() function is called automatically.

To run this controller, we have to provide following address into browser address bar:
example.com/index.php/first (If on the internet and have domain)
OR
localhost/[folder-name like codeigniter]/index.php/first (If on the local machine)

OUTPUT:
Hello World!

In general, you have to specify path as below:
Hostname/[folder-name]/index.php/Controller-name
NOTE:- folder-name: In most of case folder name is "codeigniter". You can specify your own folder name.

3. How to call function?

If you not have method/function in controller code then CodeIgniter automatically calls index() function.

Modify previous code with following code, contains one method named say_hello().

<?php
class First extends CI_Controller
{
 public function __construct()
 {
  parent::__construct();
 }
 public function index()
 {
  echo "Hello World!";
 }
 public function say_hello()
 {
  echo "Hello India!";
 }
}
?>
After modify, save this file. Again run as mentioned below:
example.com/index.php/first/say_hello (If on the internet and have domain)
OR
localhost/[folder-name like codeigniter]/index.php/first/say_hello (If on the local machine)

OUTPUT:
Hello India!

General syntax for function call is:
Hostname/[folder-name if you have]/index.php/controller-name/method-name
NOTE: If you not specify method-name then by default index() method is called.

4. How to call function with arguments?

It is simple to pass arguments to function in CodeIgniter. Modify previous code with following code.

<?php
class First extends CI_Controller
{
 public function __construct()
 {
  parent::__construct();
 }
 public function index()
 {
  echo "Hello World!";
 }
 public function say_hello($name='PHP')
 {
  echo "Hello $name!";
 }
}
?>
After modify, save it. Here our function name say_hello() contains one parameter with default argument is "PHP". Default argument is useful when user does not pass parameter, at that time inside name variable "PHP" is stored. Let's see both version.

Run above code with Parameter "Jignesh" as mentioned below:
example.com/index.php/first/say_hello/Jignesh (If on the internet and have domain)
OR
localhost/[folder-name like codeigniter]/index.php/first/say_hello/Jignesh (If on the local machine)

OUTPUT:
Hello Jignesh!

Run above code without Parameter as mentioned below:
example.com/index.php/first/say_hello (If on the internet and have domain)
OR
localhost/[folder-name like codeigniter]/index.php/first/say_hello (If on the local machine)

OUTPUT:
Hello PHP!

In general, method call with number of parameters contains following syntax:
Hostname/[folder-name if you have]/index.php/controller-name/method-name/parameter-value

If you have more than one parameter list then specify parameters with separated by /(forward-slash). For example,
Hostname/[folder-name if you have]/index.php/controller-name/method-name/parameter1/parameter2/parameterN

0 comments :

Post a Comment