CodeIgniter: Creating First Application at CodeIgniter

CodeIgniter Step By Step Tutorial: As usually, we will create first application by build hello World application. But, before write code, we must know that we will build application use Model Controller View (MVC) pattern. We will talk about MVC in other post. MVC pattern in Joomla and It is same.

1. First, we make controller, create a file name “helloworld.php” within: C:\xampp\htdocs\CodeIgniter\system\application\controllers\.
Enter following code:

<?php
class HelloWorld extends Controller{

	function HelloWorld(){
	parent::Controller();	
	}
	function index(){
	$this->load->view('index_view');	
	}
}
?>


# Next step, make a view. Create index_view.php within C:\xampp\htdocs\CodeIgniter\system\application\views.
Create View
Enter just simple line code like:

Hello,World!

# Now, test your application. Point your browser to http://localhost/CodeIgniter/index.php/HelloWorld/ You should get like this:

Show hello world In codeignitor

Here we do not write any function name in the url and it load that function because there is a index function in our controller. So it auto call the index function and load the index_view.php

Now we make one more function and call it.
Make a function like bellow

<?php
class HelloWorld extends Controller{

	function HelloWorld(){
	parent::Controller();	
	}
	function index(){
	$this->load->view('index_view');	
	}

	function hello(){
	$this->load->view('hello_view');	
	}
}
?>

Now make a new view file hello_view.php in C:\xampp\htdocs\CodeIgniter\system\application\views.
write this text in the view file

Hello,Nice to see you!

Now, test your application. Point your browser to http://localhost/CodeIgniter/index.php/HelloWorld/hello
You should get like this:
Show hello function output

See, this flow:
flow at codeigniter

 

the coder

8 Comments on "CodeIgniter: Creating First Application at CodeIgniter"

  1. Ashish says:

    After making the helloworld.php and index_view.php files when i test it in the brower it gives the following error :
    ” Fatal error: Class ‘Controller’ not found in C:\xampp\htdocs\CodeIgniter\application\controllers\helloworld.php on line 2 ”

    Since i am new to codeigniter plz help me with this issue.

  2. which version of code ignitor you use. as there is some change in new version. will you send your version name.

  3. Ashish says:

    CodeIgniter_2.0.3

  4. ok, i willl check and soon reply you.

  5. Ashish says:

    Hey,

    Sorted out this issue.
    Just prefix the Controller class as follows : “CI_Controller”

    e.g. :
    class Blog extends CI_Controller

  6. Ashish says:

    The error persists if we write the constructor.

    The following code will give error:
    fname = “Ashish”;
    $this->lname = “Picha”;
    }

    function index()
    {

    $data['fname'] = $this->fname;
    $data['lname'] = $this->lname;
    $this->load->view(‘try2_view’,$data);
    }

    }

    ?>

    Error : Fatal error: Call to undefined method CI_Controller::Controller() in C:\xampp\htdocs\CodeIgniter\application\controllers\try2.php on line 10

    only if u remove the constructor from the code and just prefix the class name by “CI” then it works.

  7. suresh says:

    Fatal error: Call to undefined method CI_Controller::Controller() in C:\xampp\htdocs\CodeIgniter\application\controllers\try2.php on line 10

    Somebody help me

  8. are you trying latest version of ci. as ci changes the controller naming.

Got something to say? Go for it!

*

 
More in beginners (8 of 10 articles)