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.

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.

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.

$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

This is the user.php file

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

This is the Users.php file in the Controller.

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

Pin It on Pinterest

Share This