CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly in the browser so, to reuse view file, we need to load or call it in the controller.

View Syntax

This is Abc.php file inside the view folder.

<!DOCTYPE
html>
 <html>
 <head>
 <meta charset = ”utf-8”>
 <title> My First Program </title.
 </head> 
 <body>
 CodeIgniter is a PHP framework for creating the web application.
 </body>
 </html> 

Loading method

A CodeIgniter provides a load() method to load the view in the Controller’s file. In this method, a view file is rendered to the Controller’ action method.

Syntax: $this-> load->view (‘filename’);

Structure of the View file

Now we will understand how we can create a view file in the views folder of the application.

Here we have written a few steps to create a view file inside the views folder.

  1. Firstly, you have to go to the View folder of the application’s folder.
Views

2. Then, Right-click on the views folder to create a subfolder. In the given below image, you will see there is a New Folder.

Note: The name of the subfolder can be the same as the controller’s file, or you can also define any name according to your choice.

Views2

3. After clicking on the New folder button, you will see the given below image. Now write the name of your folder in which you want to keep the view files. And press Enter button from your keyboard. Here, we are using the Users folder, and you can give the name of the folder according to your wish.

Views3

4. After creating the Users folder, right-click on it, then it will show the given below image. In this pop-up, there is an option New File to create the file name.

Views4

5. Now write the file name with .php extension. In the given image below, we have used the user.php file name inside the Users folder.

Views5

After creating the user.php file, now write the following code in the file.

<!DOCTYPE
html>
 <html>
 <head>
             <meta charset="utf-8">
             <title> Welcome to the Tutorials & Examples </title>
     </head> 
     <body>
     <h2 style="color:RED;" > CodeIgniter Framework </h2>
     <p style= "color:blue;"> CodeIhniter is an open source framework for developing a web application. It is based on MVC pattern </p>
     <h5 style= "color:Green;" > This is my First Example of View. </h5>
     </body>
     </html> 

Now, you have to create a Controller to call the view file in the controller’s action.

Create a Users.php file under the application/controllers/ and write the following code.

<?php
 class Users extends CI_Controller {
             public function first()
             {
         echo "This is my first Controller";  
         $this->load->view(Users/user');  // Users is the view folder in which we have passed //user.php file
     }
 }
 ?> 

$this->load->view() method is used to load the view file in the controller. So, we have to pass the folder and the file name of the view in the load method. In the above code, Users are the folder name, and the user is the file name of the view. You should not require to pass .php file extension in the load method.

Now execute the controller file to call a view file in the browser like localhost/CodeIniter-3.1.11/index.php/Users/first

Then it will show the below output:

Views6

Passing multiple views in the Controller

You can also pass the multiple view files in the same controller, as shown below.

Example

In this example, we have created two folders in views, which are Main and Users. Also, we have to create view files such as views/Main/main.php and the views/Users/user.php.

This is the main.php file

<!DOCTYPE
html>
 <html>
 <head>
             <meta charset="utf-8">
             <title>Welcome to the Tutorials & Examples</title>
     </head>
     <body> 
]     <h2 style="color:Yellow;"> This is my second view file </h2>
     <p style="color:Orange;"> You can create multiple view files and pass them to the controller</p>
     <h5 style="color:Purple;"> This is my Second Example of View. You can also design the view file according to your needs </h5> 
     <h3> //////////////////////////// </h3>
     </body>
     </html> 

This is the user.php file

<!DOCTYPE
<html>
 <html> 
 <head>
             <meta charset="utf-8">
             <title> Welcome to the Tutorials & Examples </title>
     </head>
     <body> 
     <h2 style="color:RED;" > CodeIgniter Framework </h2>
     <p style= "color:blue;"> CodeIhniter is an open source framework for developing a web application. It is based on MVC pattern </p>
     <h5 style= "color:Green;" > This is my First Example of View. </h5>
     </body>
     </html> 

Now, pass the above two view files in the Controller.

This is the Users.php file in the Controller.

<?php
 class Users extends CI_Controller {
             public function first()
             {
         echo "This is my first Controller";  
         $this->load->view(‘Users/user');  
        $this->load->view(‘Main/main’);
     }
 }
 ?> 

Now execute the above code in the browser like localhost/CodeIniter-3.1.11/index.php/Users/first

Then it will show the below output:

Views7