Showing posts with label loading. Show all posts
Showing posts with label loading. Show all posts

Saturday, January 27, 2018

Views in codeigniter ... MVC Pattern

As we know, codeigniter works on the MVC pattern, which is Models, Views and Controllers.
We have discussed in detail about the controllers in previous post. Now we are going to discuss about the Views.

First of all we should know that 

What are views in codeigniter ?

Views are considered to be the special files in Codeigniter that are used to store the markup outputted by the applications. Views are consist of HTML and PHP tags. As we all know that HTML is used in front-end designing of websites or any web application. We also know that some of the PHP tags are also used in designing the interfaces of any web application.

A view can be defined as a simple web page, or a page fragment, or any interface of web application with which the end-user is going to interact like header, footer, sidebar or body etc.

How views are created ?

Views can be created by creating a new file  in the views folder which resides in the application folder of codeigniter and saving the file with the .php extension. let say, you have created a file in views folder and saved it with name myView.php. Your View will be created.

The code in the myView.php file is as follow:

<html>
</body>
<h1> This is my View </h1>
<p> I am going to be loaded in the controller that we have created in the previous post </p>
</body>
</html>

How to load Views in Controllers?

The views that we create in the views folder are loaded in controller in order to be displayed on the web. The syntax for loading a view in the controller is as follow:

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

There is no need of giving extention of the file that is .php because codeigniter is an intelligent framework.

Full Code in controller for loading the view:

<?php class MyController extends CI_Controller {
// its the controller that we created in the previous post

public function index(){       // it is the by default function that is called by controller itself
$this->load->view('myView');     // its the view that we have created above
}
}
?>

I hope this would be a help for you. God bless you all. share this post and comment your queries, if any . Thankyou very much