How to create Controller in codeigniter?

What is controller class in codeigniter

A Controller is a class file that is control the models and view pages in your codeigniter application. in codeigniter framework controller name always starts with Capital letter such as Itutorial.php

A codeigniter controller accept the https request and perform some action and then return the result on the view pages. and finally view pages display on the browsers.

In codeigniter, All the requests received by the controller class and passed to models and views to process the action and return the results.

How to create controller class:-

The Class names must start with an uppercase letter.

File Name : Ittutorial.php

defined('BASEPATH') OR exit('no direct script access allowed');
class Ittutorial extends CI_Controller
{
public function __construct()
{
parent::__construct();

}

public function index()
{
$data = array();
$data['title'] = "Home";
$data['category'] = $this->My_model->categorylist();
$data['subcategory'] = $this->My_model->subcategorylist();

$this->load->view('header',$data);
$this->load->view('home');
$this->load->view('footer');
}
}

Controller Class

File Name : Ittutorial

defined('BASEPATH') OR exit('no direct script access allowed');
class Ittutorial extends CI_Controller
{
var $data = array();

public function __construct(){
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('My_model');
$this->load->model('Sana_model');
$this->load->model('../modules/ittutorial/models/Tutorial_model');
$this->load->helper('myhelper');
$this->load->library('zip');
include_once('../otherfunction/myfunctions.php');
// print_r($_SESSION); }

public function index()
{
$this->data['title'] = "Home";
$this->data['category'] = $this->My_model->categorylist();
$this->data['subcategory'] = $this->My_model->subcategorylist();

$this->load->view('header',$this->data);
$this->load->view('home',$this->data);
$this->load->view('footer');
}
}

Default Controller :-

if you not set default controller in your CI Application. The default controller will be loaded by default when no file name is mentioned in the URL. In Codeigniter By default, it is Welcome.php which is the first page to be seen after installing CodeIgniter.

You can change default controller through routes file

$route['default_controller'] = ' ';

File Name :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {

public function index()
{
$this->load->view('welcome_message');
}
}

Codeigniter Methods :-

The "index" method is always loaded by default if the second segment of the URI is empty. The second segment of the URL determines which method in the controller gets called.

https://ittutorial.in/mytutorial/show_name

File Name :

public function index()
{
echo "Hello Sana";
}
public function show_name()
{
echo "Hello Mahira";
}

What is Class Constructors?

parent::__construct();
parent::__construct() is local constructor will be overriding the one in the parent controller class so we need to manually call it.

File name : index.php

<?php
class Blog extends CI_Controller {

public function __construct()
{
parent::__construct();
// Your own constructor code
}
}

Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can’t return a value, but they can do some default work.





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here