Codeigniter is a small footprint MVC framework that helps programmers to create robust web applications. It is considered as one of the smallest frameworks when coming to the size and learning curve.
What are the basic concepts in codeigniter ?
Controllers
Controllers are the entry point in any web application. So controller does same duty in Codeigniter as well. It is the URL that we can access via web browser.
Creating a controller
First go to application/controllers folder and create file called Movies.php. You will find two other files already there, index.html and Welcome.php, they are shipped with Codeigniter.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Movies extends CI_Controller { public function index() { echo "hello wolrd"; } }
This Movies class will extend a class called CI_Controller, a Codeigniter core library.
Calling a controller
http://yourdomaincom/index.php/movies/index
This will print the text “hello world”. You may have noticed word movies after index.php, it is the controller name and word index is the method name. So we will call the URL like http://yourdomaincom/index.php/controller/method_name
In the next example you will see an extra method, that will be responsible for view a particular movie.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Movies extends CI_Controller { public function index() { echo "hello wolrd"; } public function view() { echo "The Movie One"; } }
You can see the hello world example in the browser
Views
Views are the real theme of the website, so it includes all the relevant html,css,js assets. We can call the view through the controllers. Let’s create a view in the application/views folder, this is the default Codeigniter view folder. Below I have added a sample code of view in Codeigniter.
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter View</title> <link href="css/styes.css" rel="stylesheet"> </head> <body> Here comes your html <div>Alert</div> </body> </html>
We can call the view through a controller as like below.
* map to /index.php/welcome/<method_name> * @see https://codeigniter.com/user_guide/general/urls.html */ public function index() { $this->load->view('index'); }
Where index is the view name, which codeigniter will find in the folder application/view/index.php, if it can’t find the file it will throw a 404 error.
If you want more cleaner approach, suppose all the movie related in one view and TV series related view in different folder you can use the below method. so this will be finding respective directories application/views/movies/index.php and application/views/tv/index.php
$this->load->view('movies/index'); $this->load->view('tv/index');
Models
Models are designed to connect with database directly. For example, you have movie database, Models will be responsible for insert,update,list, and delete movie articles.
Model classes are stored in the application/models folder. See below for a sample class in Codeigniter.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Movie_model extends CI_Model { public $title; public $content; public $date; public function get_last_ten_entries() { $query = $this->db->get('entries', 10); return $query->result(); } }
So you can see Movie_model is the class name, and the file must be saved as Movie_model.php in the application/models directory.
Loading a model
Model can be called in controller. Following code can be used to load any model.
$this->load->model('model_name');
You can call a method from the model in the controller like this
$this->movie_model->get_last_ten_entries()
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply