Most of website users using the browsers "Back" button, if they don't have Breadcrumb navigaion option. Or the websites primary navigation to return to a higher level page.
Step #1:- Create BreadcrumbComponent.php Below Php code used to create the Breadcrumb URLs. Put Breadcrumb.php file under "system/application/libraries" folder.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class BreadcrumbComponent {
private $breadcrumbs = array();
private $separator = ' › ';
private $start = '<div id="breadcrumb">';
private $end = '</div>';
public function __construct($params = array()){
if (count($params) > 0){
$this->initialize($params);
}
}
private function initialize($params = array()){
if (count($params) > 0){
foreach ($params as $key => $val){
if (isset($this->{'_' . $key})){
$this->{'_' . $key} = $val;
}
}
}
}
function add($title, $href){
if (!$title OR !$href) return;
$this->breadcrumbs[] = array('title' => $title, 'href' => $href);
}
function output(){
if ($this->breadcrumbs) {
$output = $this->start;
foreach ($this->breadcrumbs as $key => $crumb) {
if ($key){
$output .= $this->separator;
}
if (end(array_keys($this->breadcrumbs)) == $key) {
$output .= '<span>' . $crumb['title'] . '</span>';
} else {
$output .= '<a href="' . $crumb['href'] . '">' . $crumb['title'] . '</a>';
}
}
return $output . $this->end . PHP_EOL;
}
return '';
}
}
Step #2:- Use Breadcrumb in Php Controller
Load the Breadcrumb library $this->load->library('breadcrumb'); folder. append_crumb method is used to append the bread crumb items.
function Tutorial(){
parent::Controller();
$this->load->library('breadcrumbcomponent');
}
// business method
public function index(){
/* Bread crum */
$this->breadcrumbcomponent->add('Home', base_url());
$this->breadcrumbcomponent->add('Tutorials', base_url().'tutorials');
$this->breadcrumbcomponent->add('Spring Tutorial', base_url().'tutorials/spring-tutorials');
}
Step #3:- Finish :) Enjoy the How to create breadcrumb using php codeigniter
Hi I am Yashwant founder of www.technicalkeeda.com, Purpose of this website to share the programming knowledge in the form post , blogs and articles.