what is views in Codeigniter
Basis
views are resposnsible for showing the final out as HTML page.Views are loaded in controller to work on.we can have multiple view files like header,footer,index etc.Below is a simple example of creating a view file.after creating save this in your application/views/ folder.
Ex
What is Models in codeigniter
Basis
Models are optionally stored, if you want you can ignore it also.These are designed to work with database query like insert,update,create,delete and database information.Suppose you want to insert your form data in your database.Just create a model look like below in your model folder.
Ex
What is controller in codeigniter
Basis
A controller is a file where our logical operations are done.It is simply a class file that is named in a way that can be joined with a URI.These files are stored under application directory.Controllers are used to call functions by name.
Controllers begin with ‘class Test extends Controller’.Here the controller name is test.While defining the class name it should be upper case but while saving it should start with a lower case and should be saved with .php as extension name.
Ex-
<?php
class User extends Controller {
}
?>
Outline
In the above example Test is the class where we can mention our needed function. The URI will be
‘example.com/index.php/user/’.In this URI codeigniter will find the class named user.Remember your class must begin with upper case and saved with lower case.the class name and file name should match.
How data flows in Codeigniter
Basis
–Index.php file is the front controller and it must be existed to run codeigniter.
–Then router takes the advantages to decide what should be done with HTTP request.
–The cache file is sent to the browser with the normal system execution if a cache file is present
–then It is time for security check.The HTTP request is filtered for security.
–Here is now the main work to load model,libraries,helpers,plug-ins, and any other needed files according to HTTP request by the Controller,
-Then we can see the final out put on the web browser.
Displaying data dynamically in Codeigniter
Basis
Here we will see how MVC pattern work together to display information.Before reading this tutorial,first read the above title, So that you can get the result.
— create a view file under views folder and name it test_view.php to display the information.
–Create test.php in controller folder and do the programm like this
Ex
<?php
class Test extends Controller{
function index()
{
$data[‘title’] = “Practice_codeigniter”;
$data[‘language’] = “PHP”;
$data[‘framework’] = array(‘Sympfon’,’Zend’,’Codeigniter’);
$this->load->view(‘test_view’,$data);
}
function comments()
{
echo ‘Is it my comment!’;
}
}
?>
Outline
–Here we have passed some of the data types under the function .Here we have added $data to load all the data and we can use the title,language,framework as a variable in the view file.
–Now we have to call all this data in our view files and here is an array and for this we have to create a control structure
–Just type this code in the view file(test_view.php)
Ex
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $language;?></h1>
<ol>
<?php foreach($framework as $item){ ?>
<li><?php echo $item;}?></li>
</ol>
</body>
</html>
outline
–Here all the variables are called to display the information .
–Now go to browser and get your result.
Hello world programme in Codeigniter
Basis
Like any other programming Here we will see how we can write a simple Hello world programme.
Create a controller file under controller folder called test.php.This name should match with the calls name.
The calss name should be started with capital letter whether the file name will be lower case.
Ex
<?php
class Test extends Controller {
function index()
{
echo ‘Hello World!’;
}
}
?>
Outline
—The class name here extended with parent Controller class .It is necessary to do because the local class will inherit all the properties and attributes of parent controller class available by CodeIgniter.
–index is the default function in this class.
–Then here is a echo statement to display
–Now go to browser and type example.com/index.php/test/ you will see the out put.
–According to the MVC approach the class name should start first and than the function name.If the function name remains blank that is called automatically by the class name in the browser.
–If we want our out put when typing only index.php in the browser,we have to change something in CodeIgniter
–Go to application->config->routes.php and change the default controller to that controller(test).
–Now if you type example.com/index.php and reload the page it will open.
What is MVC approach in Codeigniter
–Before starting Code Igniter we have to understand what is MVC.MVC means Model, View and controller(directories)
–These three directories are available under CodeIgniter’s system->application folder.
–All the database linked files are stored in Model(insert,fetch,query).
–Programm files are stored in controller.
—A Controller is a PHP class file and the main part of application because they say how HTTP requests is controlled,
–Under views we can store our view files(HTML Pages) to display the content and we can fragment it as header,footer and sidebar etc.
–errors folders stores the templates to shows the error messages.
Understanding file structure in Codeigniter
There are four files and directories under CodeIgniter.Those are system,user_ guide,index. php, licence.txt
–Index.php file is the main controller file without this file CodeIgniter won’t work.
–The application folder is the main folder inside the system folder. where our web sites all files are kept Out of all folder available under it,the important are Models,views and controller directories.
CodeIgniter installation guide on Windows
— Go to http://www.codeigniter.com/
— Download the latest version of CodeIgniter.
— Unzip the zipped file and rename as you want.
—Place it under www of wamp installed folder.
—To access just type the root path for i.e -http://localhost/codeigniter/
—Then you will be able to see the welcome message codeignter.
About Codeigniter
Codeigniter is a popular framework of PHP for web development purpose. Codeigniter is a tool to make the PHP code more cleaner,easier and lesser. If we choose Code igniter for our project we can save our valuable time by decreasing the code.It has very large library and helpers for fast work.It makes the site robust.
It keeps all links up-to-date automatically.we can take benefit by saving bandwidth by zipping files which is download by users.To learn codeigniter we should have minimum knowledge of HTML, PHP,CSS.