The View is responsible for presenting data from the Model in a specific format, typically using HTML. Views are simple web pages that display data passed from the Controller, providing a clear separation between the application logic and the presentation layer. This tutorial will guide you through the process of creating, loading, and dynamically adding data to views in CodeIgniter 3.
What is a View in CodeIgniter?
A View in CodeIgniter is essentially a PHP file that contains HTML and PHP code used to display the data. It is responsible for rendering the user interface of your application. Views are typically loaded by a Controller, which processes the data and passes it to the View for presentation.
How to Create a View in CodeIgniter
1. Create a basic HTML file to act as your View. Below is an example of a simple view named about_us.php
:
<html> <head> <title>About Us</title> </head> <body> <h1>Welcome to infovistar</h1> </body> </html>
2. Save this file in the application/views
directory of your CodeIgniter project.
How to Load a View in CodeIgniter
To load a view, use the $this->load->view()
method in a Controller. This function takes the name of the view file (without the .php
extension) as its parameter.
For example,
$this->load->view('about_us');
This line will load the about_us.php
file from the application/views
directory. The .php
extension does not need to be specified unless the view file has a different extension.
Loading a View in a Controller
Let’s set up a Controller to load the about_us
view. Create a new Controller named Welcome.php
in the application/controllers
directory with the following code:
class Welcome extends CI_Controller { public function about_us() { $this->load->view('about_us'); } }
Adding Dynamic Data to the View
To make your views more dynamic, you can pass data from the Controller to the View using an array or an object. This is done by passing the data as the second parameter of the $this->load->view()
method.
Here is an example of passing data using an array:
$data = array( 'title' => 'infovistar.com', 'heading' => 'About Us', 'message' => 'Welcome to CodeIgniter' ); $this->load->view('about_us', $data);
1. Create the Welcome.php
file in the application/controllers
directory and include the about_us
method that passes data to the view:
class Welcome extends CI_Controller { public function about_us() { $data = array( 'title' => infovistar.in', 'heading' => About Us', 'message' => 'Welcome to CodeIgniter‘' ); $this->load->view('about_us', $data); } }
2. Create the about_us.php
file in the application/views
directory. This file will use the dynamic data passed from the Controller:
<html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $heading; ?></h1> <h6><?php echo $message; ?></h6> </body> </html>
How It Works
- Access the View: Open your browser and navigate to
http://yourdomain.com/welcome/about_us
. This will call theabout_us
method from theWelcome
controller. - Controller Loads the View: The
about_us
method in theWelcome
controller loads theabout_us
view and passes the dynamic data array to it. - View Displays the Data: The
about_us.php
view receives the data from the Controller and displays it in the specified HTML format.
Views play a crucial role in separating the presentation layer from the application logic.