After learning about the CodeIgniter Controller now we need to understand the Controller Views. This is a simple web-page which contains HTML, CSS, JS and PHP codes or any other code which you need for the development of your Application GUI.
you can also be used Views as the fragment within another view. All the views are called by the controllers.

You need to know that views can’t be loaded directly without the controller. Let’s create a sample view. You have to open a view folder within the application folder. You will found errors folder which contains the views for errors such as default view or 404 error view.

By default there is “index.html” and “welcome_message.php” file is available. You don’t need to change these files if you are just beginner with CodeIgniter. But if you have some knowledge about how it’s work you can easily manipulate files.

Now create a new file for your own view, in our case we are going to create the file having the name as “ControllerView.php”. The code is as follows.

<!DOCTYPE html>
<html>
<head>
  <title>TutorialPace.com CodeIgniter View</title>
</head>
<body>
  <div>
    <p>Simple text</p>
  </div>
</body>
</html>

If you want to execute this view in the browser you need to add the reference in your controller as we have done in the following code.

<?php 
   class CodeigniterController extends CI_Controller{

   	public function index() { 
         echo "Welcome to TutorialPace.com";
         $this->load->view('ControllerView'); 
    }

    public function page() { 
         echo "This is page action listner"; 
    }

    public function blog() { 
         echo "This is blog action listner"; 
    }  

   } 
?>

Results in browser:

View loading by Controller

If you have created a view within a subfolder of view folder then you have to mention that folder in the controller when you are loading the view.

In the above code, you will observe that the below code is used to load the view from the controller in the browser.

$this->load->view('ControllerView');

In case of view file within another subfolder of view folder.

$this->load->view('subfolder/ControllerView');

You may also notice that we are not using PHP extension when we are calling a view within the controller.

Important points:

  • You can use multiple views within the same controller such as first load header then body and then footer view.
  • You can pass view within another view from the controller as a parameter.
  • You can render front and quote from a controller within any other view as a parameter.