Friday, November 13
Trending
- Microsoft 365 Outage: Outlook, Teams and Copilot Down. What We Know
- Diwali Online Shopping Scams 2026: 10 Ways to Stay Safe
- 10 Best DDoS Protection Services for 2027
- 12 Best Web Application Firewalls (WAF) for 2027
- Best Ethical Hacking Tools in 2027: Free & Paid Picks
- Software supply chain attacks 2026: Timeline and developer checklist
- Binance Hack: $6.3 Billion Drained in the Largest Crypto Theft Ever. What We Know So Far
- This Site Uses XSLT Chrome Warning Explained: How to Fix It
The View is responsible for the presentation of the Model (data) in a certain format. A view is a simple web page created using HTML. We can call a view by using a Controller.
How to create a view?
<html>
<head>
<title>About Us</title>
</head>
<body>
<h1>infovistar.com</h1>
</body>
</html>Save the file as about_us.php in your /app/Views directory.
How to load a view?
The view() method is used to load a view in a Controller.
| Load a view in CodeIgniter 3 | Load a view in CodeIgniter 4 |
|---|---|
| $this->load->view(‘about_us’); | echo view(‘about_us’); |
Now we can set this view to our about_us() method in the Home.php controller.
<?php
namespace App\Controllers;
class Home extends BaseController {
public function about_us() {
echo view('about_us');
}
}
?>Add dynamic data to the View
We can pass data from the controller to the view by way of an array or an object in the second parameter of the view method.
Here is an example using an array:
$data = array(
'title' => infovistar.com',
'heading' => About Us',
'message' => 'Welcome to CodeIgniter‘'
);
echo view('about_us', $data);Example:
Controller: Home.php
<?php
namespace AppControllers;
class Home extends BaseController {
public function about_us() {
$data = array(
'title' => 'infovistar.com',
'heading' => 'About Us',
'message' => 'Welcome to CodeIgniter'
);
echo view('about_us', $data);
}
}
?>View: about_us.php
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $heading; ?></h1>
<h6><?php echo $message; ?></h6>
</body>
</html>


