CodeIgniter – MVC Framework

Posted on December 2, 2019 By

Codeigniter is PHP Framework which is construct on the Model-View-Controller (MVC) architectural pattern. Model-View-Controller is a software development pattern which divides application in three part the model, the view and the controller.

Model– The Model contains the application data structures. These contain different sets of the functions related to database operation that helps in inserting, updating and deleting information in the database.

Models are created in application/models directory.

Syntax:

class Model_name extends CI_Model
{
	public function __construct() { 
         parent::__construct(); 
    } 

}

Description:

Model_name is a name of module. All model class extends CI_Model class which is a CodeIgniter class.

Example:

class Product_model extends CI_Model
{
	public function getProducts()
    {
    	$products = array();
    	return $products;
    }
}

Description:

Here we declared new class with name Product_model. This class have a public function getProducts which returns product array.

This Product_model class will save in php file named with Product_model.php means class name and file name will be same. For Example If you want to create Category_model then you must save it within file Category_model.php.

How to use Model?

Syntax:

$this->model_name->method();

Where model_name is name of model, method() is a name of method.

so to call getProducts method of Product_model you can declare as below:

$this->Product_model->getProducts();

View– The view is the user interface (UI) that user will see. A view in Codeigniter is web page front design for user to show information and different form like elements for submit user information.

Normally view are located under application/views directory.

Example:

<!DOCTYPE html>
<html>
	<head>
		<title>CodeIgniter View Introduction</title>
	</head>
	<body>
		<h1>This is a heading</h1>
		<p>This is a paragraph</p>
	</body>
</html>

How to load View?

Syntax:

$this->load->view('view_name');

Example:

To load welcome_page view below code given.

$this->load->view('welcome_page');

Sometime view is under sub directory then to call view below code used

$this->load->view('directory_name/view_name');

Where directory_name is name of directory under which view is located. view_name is name of view which you want to call.

Controller– The controller is a mediator between model and view. All logical part of web application is implemented in controllers. When user request for get some information then controller will get information using model and send it to view to show result information to user.

Controller are located under application/controllers directory.

Syntax:

class Controller_name extends CI_Controller {
        public function index()
	{
	     $this->load->view('welcome_page');
	}
}

Where Controller_name is a name of controller. It extends global controller CI_Controller which is main controller of codeigniter. Then define method in it.

Example:

class Welcome extends CI_Controller {
        public function index()
	{
	     $this->load->view('welcome_page');
	}
}

Here we have created controller Welcome and define index method which will load view page welcome_page. Controller will save in Welcome.php file. So the controller name file name will be same and name must start from capital letter.

Leave a Reply

Your email address will not be published. Required fields are marked *