Jan 9, 2012

Learn Codeigniter | Codeigniter Tutorial | Codeigniter Lessons | Part 1 | Webs Tutorial

What is CodeIgniter?

CodeIgniter is an open source PHP framework. This framework is used to build web applications and websites. CodeIgniter is best for developers who are into front-end development. It’s easy to learn and it’s fully flexibe.

To learn CI (CodeIgniter), you should know PHP, MYSQL, OOPs and MVC. If you are new to PHP and MYSQL, then I would recommend you to first learn it, you can learn from here.

What is MVC?

If you have selected to head start with CI, then you should know MVC.

MVC is nothing but a flow of code, it tells the framework which code to execute, at what time, and where to show the executed code. Normally when you create a simple php file which accesses the database, queries data and display it on index.php page, at that time all looks clear and simple. Now imagine you have a huge database, about 100+ SELECT queries, and suddenly your boss rings a bell and asks you that where is this query??? If at that time you would do CTRL+F and find that query statement, then you are really in a mess. MVC makes your job easy. MVC is a model, view and controller. When a request is triggered from client side using a view, that request reaches directly to the controller, controller decides whether it is a database request or simple HTML request. If it’s a database request then, it passes the essential code to model, a model handles all database calls and requests. Model interacts with database, gets the data and passes again to controller. Then controller passes the data to view and finally the flow ends. If the request was an html request, then controller directly passes the data to view.

It is simple, a view is war ground. A controller is the headquarters and a Model is an inventory base.

If I have confused you in understanding the flow of MVC, then you can refer this image.

MVC

Now let’s start the CI, first download the latest version of CI from here.

Before I start further, we need following things to begin.

  • Localhost server: I am using WAMP

  • Text Editor: I am using Eclipse


After download completes, you would be having a zip file, extract that into www folder, rename that folder to ci or your project name. Go inside system folder, cut application folder and paste it inside CI folder.

So your CI folder structure should be like this:

ci-folder-structure

Now in your browser run the following URL: http://localhost/ci/

It would display the default CI welcome page. If everything goes well till here, then you have successfully installed CI.

Let’s start first with a controller.

Create a new file inside controller folder, name it as home.php

MVC is made in OOPs, so we need to make a class inside our home.php file.

[php]
<?php

class home extends CI_Controller
{
function index(){
echo 'Welcome to CI Home';
}
}

?>
[/php]

Remember: Never echo anything inside our controller but as this is our first example so just to understand the MVC pattern we are doing it.

In the above function we made a class which extends the Controller class of CI. In CI the function named with index is loaded as default function. So when there are many functions inside a class, the function named as index loads the first.

Now just run this program: http://localhost:8080/ci/index.php/home

You can see your newly created controller.

Now suppose we had another function inside our controller, then how would we access it?

Just add one more parameter in URL after /home/

So let’s create one more function within our controller.

[php]
function another(){
echo 'Welcome to CI Home - This is another function within the same class of home controller';
}
[/php]

Access this function simply by adding the function name to the URL

http://localhost:8080/ci/index.php/home/another

When into some project we should never echo out any data or string from our controller, instead we should use controller to access the database through model or load some views, or in some case both.

At this part we have understood controller, now let’s understand a view.

In CI, many people say to follow naming conventions for naming the files. But you can name to view as per your choice.

Create a new file inside views folder. Name it home-view.php

home-view.php:

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome To Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Home View loaded successfully!!!</p>
</body>
</html>
[/html]

We will invoke the view through a controller.

Edit our home controller, delete our old functions and add a new index function.

In this example we are actually loading a view from a controller.

Home controller Index function:

[php]
function index()
{
$this->load->view('home-view');
}
[/php]

CI has libraries and classes which help us to write less and do more. The basic syntax to invoke a view is $this->load->view(‘Name of the view file’);

Run the code in browser and check it

URL http://localhost:8080/ci/index.php/home/

So now you are able to see the entire view.

Till now we just worked with Controller and a view. The only thing remaining is the model. But before we start with model let’s do some more within our controller and view.

Now we will pass some variables from controller to our view.

Within our index function, let’s create an array and store some values into it.

This array will be passed to our view.

[php]
function index()
{
$data['value1'] = 'My first Value';
$data['value2'] = 'My second Value';
$this->load->view('home-view', $data);
}
[/php]

In CI to pass some data to view, we simply add it as a second parameter in our load view line.

So in our view, we can directly access to value1 and value2 without getting into any loops

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Welcome To Home</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Home View loaded successfully!!!s</p>

<?php
echo $value1 . '<br>';
echo $value2;
?>
</body>
</html>
[/html]

I explained this example instead of heading towards model, because it’s essential for you to understand how values are passed and how they are accessed in our view.

Here I end part 1 tutorial of CI, I recommend you to do some more examples within this tutorial so that in next part you would be use to CI. In our next tutorial we are going to understand the entire MVC which will interact with database. After understanding the basic use of CI, we will start making our first CMS or a website in CI framework which will have Insert, Update and Delete functionality.

Tips: In CI, if you want to make a controller function private, then just pre-pend _ before the function name.

[php]
function _anything()
{
$this->load->view('private-view', $data);
}
[/php]

4 comments :

  1. please please carry this tutorial. Please Please Please

    ReplyDelete
  2. [...] What is CodeIgniter? CodeIgniter is an open source PHP framework. This framework is used to build web applications and websites. CodeIgniter is best for developers who are into front-end development. It’s easy to learn and it’s fully flexibe. To learn CI (CodeIgniter), you should know PHP, MYSQL, OOPs and MVC. If you are new to Read more… [...]

    ReplyDelete
  3. velocitysoftware217/3/14 6:22 PM

    Thank You , It's Very Good Examples now pleas learn me how to work with model

    ReplyDelete