For any queries you can reach us at infovistarindia@gmail.com / WhatsApp us: +919158876092

Include header and footer in CodeIgniter 4

How to include header footer

First of all, create a common file in the view folder. In this example, I have created a file name as template.php in /app/Views/innerpages directory.

File structure:
/app/Views/
	+ innerpages
		- footer.php
		- header.php
		- template.php
	- home.php

/app/Controllers
	- Home.php

/app/Views/innerpages/template.php

<?php 

	echo view('innerpages/header.php');
	echo view($main_content);
	echo view('innerpages/footer.php');

?>

In the above example, $main_content is a dynamic view for each page.

/app/Views/innerpages/header.php

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>

/app/Views/innerpages/footer.php

<h1><?php $heading; ?></h1>

</body>
</html>

/app/Controllers/Home.php

namespace App\Controllers;
use App\Controllers\BaseController;

class Home extends BaseController {

	public function index() {
		$data = [];
		$data['title'] 		= 'Page Title';
		$data['heading']	= 'Welcome to infovistar.com'
		$data['main_content']	= 'home';	// page name
		echo view('innerpages/template', $data);
	}

}

/app/Views/home.php

<h4> Welcome to Home </h4>