- A view is a web page that display all the elements of User Interface. View deals with the fronted operations. View display data and capture user action. View send user action to controller. Location of all view files is applications/views.
View could not be called directly. You need to load the view through controller.
How to create view
Create a HTML page and name it firstview.php. Save this file inside applications/views.
Code of the firstview.php file.
<html> <head> <title>CodeIgniter View</title> </head> <body> <h1>Welcome to PHPGurukul Programming Blog</h1> </body> </html>
Load the view
View will be load through controller. Syntax for loading view will be :
$this->load->view('name of the view');
For our case syntax will be :
$this->load->view('firstview');
Load multiple views in Controller
<?php class Blog extends CI_Controller { public function index() { $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); } }
You can also create a new folder inside application/view and save your files in this folder.
Sort views in Subfolders
You can sort views within subfolders:
$this->load->view(‘directory_name/file_name’);
Pass Dynamic Data to view
Data can be transfer from controller to view by using an array or object.
<?php class Blog extends CI_Controller { public function index() { $data['title'] = "CodeIgniter View"; $data['heading'] = "Welcome to PHPGurukul"; $this->load->view('firstview', $data); } }
View will be look like this :
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> </body> </html>