How to include PHP files/Templates in CodeIgniter

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this tutorial. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_include so I'm using the below code.
  1. localhost/codeigniter_include
codeigniter successful install

Configuring our Base URL

Next, we configure our base url which literally the link our our site or application. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff line:
  1. $config['base_url'] = 'http://localhost/codeigniter_include';

Creating our Controller

Next, we create our controller. This is where we create the files that we want to include in a view by passing it when calling the view function. Create a file named Routes.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Routes extends CI_Controller {
  5. function __construct(){
  6. parent::__construct();
  7. $this->load->helper('url');
  8.  
  9. $this->inc['title'] = 'How to include PHP files/Templates in CodeIgniter';
  10. $this->inc['navbar'] = $this->load->view('navbar', '', true);
  11. }
  12.  
  13. public function index(){
  14. $this->inc['page_title'] = 'Home';
  15. $this->load->view('home', $this->inc);
  16. }
  17.  
  18. public function about(){
  19. $this->inc['page_title'] = 'About';
  20. $this->load->view('home', $this->inc);
  21. }
  22.  
  23. public function blog(){
  24. $this->inc['page_title'] = 'Blog';
  25. $this->load->view('home', $this->inc);
  26. }
  27.  
  28. }
In here we define the files to be included inside $inc of our class.

Defining our Default Route

Next, we are going to set our default route so that whenever we haven't set up a controller to use, this default controller will be used instead. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
  1. $route['default_controller'] = 'routes';

Creating our Views

Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using this link. Create the ff files inside application/views folder. home.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title><?php echo $title; ?></title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <?php echo $navbar; ?>
  10. <div class="container">
  11. <div class="jumbotron">
  12. <h1 class="text-center">This is the <b><?php echo $page_title; ?></b> page</h1>
  13. </div>
  14. </div>
  15. </body>
  16. </html>
about.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title><?php echo $title; ?></title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <?php echo $navbar; ?>
  10. <div class="container">
  11. <div class="jumbotron">
  12. <h1 class="text-center">This is the <b><?php echo $page_title; ?></b> page</h1>
  13. </div>
  14. </div>
  15. </body>
  16. </html>
blog.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title><?php echo $title; ?></title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <?php echo $navbar; ?>
  10. <div class="container">
  11. <div class="jumbotron">
  12. <h1 class="text-center">This is the <b><?php echo $page_title; ?></b> page</h1>
  13. </div>
  14. </div>
  15. </body>
  16. </html>
navbar.php This is our included navbar.
  1. <nav class="navbar navbar-default">
  2. <div class="container">
  3. <div class="navbar-header">
  4. <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
  5. <span class="sr-only">Toggle navigation</span>
  6. <span class="icon-bar"></span>
  7. <span class="icon-bar"></span>
  8. <span class="icon-bar"></span>
  9. </button>
  10. <a class="navbar-brand" href="https://www.sourcecodester.com">SourceCodester</a>
  11. </div>
  12. <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
  13. <ul class="nav navbar-nav">
  14. <li><a href="<?php echo base_url(); ?>index.php/routes/index">Home</a></li>
  15. <li><a href="<?php echo base_url(); ?>index.php/routes/about">About</a></li>
  16. <li><a href="<?php echo base_url(); ?>index.php/routes/blog">Blog</a></li>
  17. </ul>
  18. </div>
  19. </div>
  20. </nav>
That ends this tutorial. Happy coding :)

Add new comment