A controller is a simple class file. it controls the whole application by URI.
First, go to application/controllers folder. You will find two files there, index.html and Welcome.php. These files come with the CodeIgniter. Keep these files as they are. Create a new file under the same path named "Mytest.php". Write the following code in that file −
The Test class extends an in-built class called CI_Controller. This class must be extended whenever you want to make your own Controller class.
The above controller can be called by URI as follows −
http://www.ittutorial.in/index.php/test
Notice the word “test” in the above URI after index.php. This indicates the class name of controller. As we have given the name of the controller “Test”, we are writing “test” after the index.php. The class name must start with uppercase letter but we need to write lowercase letter when we call that controller by URI. The general syntax for calling the controller is as follows −
http://www.ittutorial.in/index.php/controller/method-name
We can execute the above controller in the following three ways −
view is a webpage, which can be called by the controller. View cannot be called directly. Create a new file under application/views with name mytest.php and copy the below given code in that file.
A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.
The view can be loaded by the following syntax −
$this->load->view('name');
Where name is the view file, which is being rendered. If you have planned to store the view file in some directory then you can use the following syntax −
$this->load->view('directory-name/name');
It is not necessary to specify the extension as .php, unless something other than .php is used.
The index() method is calling the view method and passing the “test” as argument to view() method because we have stored the html coding in “mytest.php” file under application/views/mytest.php.
<?php
class Test extends CI_Controller {
public function index() {
$this->load->view('mytest');
}
}
?>
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
Now open your view file and change the text to variables that correspond to the array keys in your data:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>
The data array you pass to your view files is not limited to simple variables. You can pass multi dimensional arrays, which can be looped to generate multiple rows. For example, if you pull data from your database it will typically be in the form of a multi-dimensional array. Here’s a simple example. Add this to your controller:
Models classes are designed to work with information in the database. As an example, if you are using CodeIgniter to manage users in your application then you must have model class, which contains functions to insert, delete, update and retrieve your users’ data.
Model classes are stored in application/models directory.
Where Model_name is the name of the model class that you want to give. Each model class must inherit the CodeIgniter’s CI_Model class. The first letter of the model class must be in capital letter.
Model can be called in controller. Following code can be used to load any model.
$this->load->model('model_name');
Where model_name is the name of the model to be loaded. After loading the model you can simply call its method as shown below.
$this->model_name->method();
$autoload['model'] = array();
// $autoload['model'] = array('first_model', 'second_model');
Trending Tutorials