We will integrate bootstrap theme with CodeIgniter using layout. This tutorial help to create own custom layout for CodeIgniter. This is a master template that will use to render a child view file, like each website template has header, footer and sidebar view which is constant and nothing has been changed between different pages.
We will define a master layout, which has constant things as its and variable things like inner content will change based on routing or request of page.
The first question is in our mind what will be the variable in the CodeIgniter layout?, the page title, logo message, page content etc, we will use page title and content as a variable in this Codeigniter tutorial.
There are following library and frameworks must be downloaded and configured
mod_rewrite
module(for Apache)I am using bootstrap admin theme to create the CodeIgniter template. You can change the theme as per your application need. The CI templates integration process would be same like any other HTML theme.
I am creating ci_test
folder into xampp/htdocs/
directory and moved all files and directories from CodeIgniter 3 downloaded folder to xampp/htdocs/ci_test
folder.
We will create and modify the following CI files.
template.php
class.Modify config.php file
We will modify some configuration parameters in application/config/config.php
file, I made the following changes to this file:
$config['uri_protocol'] = 'REQUEST_URI'; $config['base_url'] = 'http://ci_test/'; $config['index_page'] = ''; //remove index.php file from url
Added .htaccess file
We will add .htaccess
file to the route of /ci_test
folder. We will write some rules for SEO and User-Friendly URLs. You can access the employee list using this http://ci_test/employee
url instead of http://ci_test/index.php/employee
.
RewriteEngine on RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Modify autoload.php
We will modify autoload.php
file for template library class. We will add a helper library class in this file. You can find changes later in this tutorial.
Let’s change the default controller name from welcome to home, whenever a page will load into the browser without the controller name, The default controller will be home and index()
method will be call.
$route['default_controller'] = 'home';
Step 1: We will create a template class for rendering the template view file. Created a template.php
file and stored into the /library
folder.
<?php class Template { //ci instance private $CI; //template Data var $template_data = array(); public function __construct() { $this----->CI =& get_instance(); } function set($content_area, $value) { $this->template_data[$content_area] = $value; } function load($template = '', $name ='', $view = '' , $view_data = array(), $return = FALSE) { $this->set($name , $this->CI->load->view($view, $view_data, TRUE)); $this->CI->load->view('layouts/'.$template, $this->template_data); } } ?>
Step 2: We will create a default_layout.php
view file into the views/layouts/
folder.This file will contains all js, css libraries files with html inner container variable.
<title>Test CI Application - <?php echo $title;?></title> <meta name="description" content="overview & stats"> <!-- bootstrap & fontawesome --> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/font-awesome/4.2.0/css/font-awesome.min.css"> <link rel="stylesheet" href="<?php echo base_url();?>assets/css/theme.min.css" class="theme-stylesheet" id="theme-style"> <link rel="stylesheet" href="<?php echo base_url();?>assets/fonts/fonts.googleapis.com.css"> <!-- page specific plugin styles --> <div id="navbar" class="navbar navbar-default"> </div> <div class="page-content"> <div class="row"> <div class="col-xs-12"> <!-- PAGE CONTENT BEGINS --> <?php echo $contents;?> <!-- PAGE CONTENT ENDS --> </div> <!-- /.col --> </div> <!-- /.row --> </div> <!-- /.page-content --> <!-- /.main-content --> <div class="footer"> <div class="footer-inner"> <div class="footer-content"> <span class="bigger-120"> Copyright © phpflow.com. All rights reserved. </span> </div> </div> </div> <a href="#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse"> <i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i> </a> <!-- /.main-container --> <!-- basic scripts --> <script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery.2.1.1.min.js"></script> <script src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script> <script src="<?php echo base_url();?>assets/js/theme.min.js"></script>
Step 3: Added template entry into config/autoload.php
file to load template class when the CI has been initialized.
$autoload['libraries'] = array('template'); $autoload['helper'] = array('url');
We have made a change in the config for the default controller which was home, so we will create a new php file Home.php
file into the /controllers folder. Also created an index()
method into it. Please add the below code into Home.php
controller file.
public function index() { header("Access-Control-Allow-Origin: *"); $data = array(); $this->template->set('title', 'Home'); $this->template->load('default_layout', 'contents' , 'home', $data); }
We have defined home view file in index()
controller method. We will create a new home.php
file into views/
folder. Please add the below code into this file,
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> Lorem Ipsum ...... Ipsum.
We will add a new page to this codeigniter tutorial
, So that you can understand the use of layout.Let’s create a about page that will render on the same page layout. We just send the view to the Default Layout file and rest of the theme structure same as the home.
Step 1: Create a new entry route into route.php
file.
$route['home/about'] = 'home/about';
We have mentioned home
is controller and about
is a method.
Step 2: Created about method into Home.php
controller file.
public function about() { $data = array(); $this->template->set('title', 'about'); $this->template->load('default_layout', 'contents' , 'about', $data); }
Step 3: Created about.php
view file into views
folder.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> It is a long .......
This is a codeigniter beginners tutorial help to integrate beautiful bootstrap theme with Codeigniter using Layout.You can also integrate simple HTML theme using this codeigniter tutorial.We have create new controller and view file and render them using template.
You can download source code from below link.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
Hello,
Please explain the purpose of the 5th parameter in your load method.
function load($template = '', $name = '', $view = '', $view_data = array(), $return = FALSE)
I don't see anywhere that it's being used....
remove and see whats happened
An Error Was Encountered
Unable to load the requested class: Template
Change name file into libraries folder to Template.php
the link to Download Bootstrap Theme Bootstrap Admin Theme. is blank plz help
i fixed that
Thx for this tutorial. I've the problem, that the Images and the font wont be displayed.
This is a very informative article. Thanks for sharing these helpful articles.