Close

🔰 PHP Programming Tutorial 9 - 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 echo $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 chirags.in'; 		
			$data['main_content']	= 'home';	// page name 		
			echo view('innerpages/template', $data); 	
		}  
	}

/app/Views/home.php


<h4> Welcome to Home </h4>


😉Subscribe and like for more videos:
https://www.youtube.com/@chiragstutorial
💛Don't forget to, 💘Follow, 💝Like, 💖Share 💙&, Comment

0 Comments
Leave a message

 

Search Current Affairs by date
Other Category List

Cookies Consent

We use cookies to enhance your browsing experience and analyze our traffic. By clicking "Accept All", you consent to our use of cookies. Cookies Policy