-
Home
-
Blogs
-
Tutorial-2-codeigniter-php-framework -
Tutorial - 2 CodeIgniter PHP Framework
By Ankur Rajput | Jul, 10 2018 06:11
Hi guys, This tutorial based on PHP framework (CodeIgniter), Today we learn how to create controllers models and views.
1. How to create controller.
Then follow these steps...
- Goto application -> controllers dir.
- This directory contains all controllers of your projects.
- Lets create Home.php file in your notepad++ or else then save it in controller diretory.
- Make sure the your first letter of your file name is capital for ex- Home.php.
- Now create class using following source code...
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
public function index()
{
}
?>
- In the above code Home is a class name which extent CI_Controller class.
- function __construct() is the constructor in which code can write need to run first.
- public function index() this is public function and name index(), by default controller called index function/action.
- Write you php code that you want to perform for ex-
public function index()
{
echo "this is my first controller";
}
2. Now, we create model in which you can write your mysql quires.
- Goto application -> model directory.
- In this location you can store your all model file for ex...
- Now create first model. As earlier we discuss model file also in .php file,
- Now, open your notepad++ or else editor then type the follow code...
if (!defined("BASEPATH")) exit("No direct script access allowed");
class HomeModel extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$this->db->select("*");
$this->db->from("customer");
$query = $this->db->get();
return $query->result();
}
}
- if (!defined("BASEPATH")) exit("No direct script access allowed"); this line protect your file to avoid direct access.
- class HomeModel extends CI_Model as we discussed on class name First letter should be in capital which extents CI_Model class.
-
public function __construct(){ this line create constructor for class in which we load database files which $this->load->database(); this line.
-
public function index() { as we discuses earlier this action/function where you can write your database query.
-
$this->db->select("*"); this is by default CI function to get all data from table.
-
$this->db->from("customer"); here give name of table ex- customer table.
-
$query = $this->db->get(); this function allow to execute CI query.
-
return $query->result(); this function give you data from table in the form of (object), or if you want to get data in form of array using $query->result_array();
-
then finally return data to controller.
3. Now then 3rd step in which we can create html file.
- As we discussed in previous post views directory contains html/ php file.
- Goto application->views
- Write your HTML template that you want to display on web page.
- Something like this..
Demo Tutorial on CodeIgniter
- Then open your Home controller and write code like...
public function index()
{
$this->load->view("demo");
}
- $this->load->view("demo"); this function is ude to load view/html template on your controller.
4. Then Open your browser and type http://localhost/CI/Home
If your screen looks like as above Image, its means you successfully done your JOB.
I hope you like this tutorial.
On next tutorial we will discuss on how to get data from database using model and display on HTML page using controller.
0 comments