CodeIgniter libraries that don’t come with the framework are stored inside application/libraries directory. Libraries are classes that add functionalities to your framework. You could ask: Why make a library and not a helper. Make_bread will be a library because is not a simple collection of functions, but methods that work with each other.

Thinking before working

Before we get to work let’s think about what we want to accomplish. The end result of our library would have to allow us to create a breadcrumb with the “crumbs” that we provide. Also it will have to be well formed: have a container (<div> or <nav>), and have separators between the “crumbs”.

The prototype – doing the first draft of our library

To start with, let’s create a file named Make_bread.php inside application/libraries. In it we will have to write the basic format of a Codeigniter library class:

Cool. Now what do we need? We need to put our library to work. What methods do we need:

A method that adds crumbs to the breadcrumb. It will receive the title, the address (optional), and TRUE or FALSE (also optional which defaults to FALSE) if the links are going to really be crumbs. I will explain more below. So the format would be: add($title, $href = NULL , 1)

A method that outputs the breadcrumb. The format would simply be output();

1. The add() method

The add() method will allow us to add “crumbs” to the breadcrumb. The method must receive at least one parameter. The first parameter must be the title of the link. As a second parameter, which is optional, the method can receive the url. Let’s allow the user to put as URL a relative path to the site. Also, if we know the breadcrumb will have, for example, the format:

// if the method won't receive the $title parameter, it won't do anything to the $_breadcrumb

if(is_null($title))return;

// first let's find out if we have a $href

if(isset($href)&&strlen($href)>0)

{

// if $segment is not FALSE we will build the URL from the previous crumb

if($segment)

{

$previous=$this->_breadcrumb[sizeof($this->_breadcrumb)-1]['href'];

$href=$previous.'/'.$href;

}

// else if the $href is not an absolute path we compose the URL from our site's URL

elseif(!filter_var($href,FILTER_VALIDATE_URL))

{

$href=site_url($href);

}

}

// add crumb to the end of the breadcrumb

$this->_breadcrumb[]=array('title'=>$title,'href'=>$href);

}

publicfunctionoutput()

{

// we open the container's tag

$output=$this->_container_open;

if(sizeof($this->_breadcrumb)>0)

{

foreach($this->_breadcrumb as$key=>$crumb)

{

// we put the crumb with open and closing tags

$output.=$this->_crumb_open;

if(strlen($crumb['href'])>0)

{

$output.=anchor($crumb['href'],$crumb['title']);

}

else

{

$output.=$crumb['title'];

}

$output.=$this->_crumb_close;

// we end the crumb with the divider if is not the last crumb

if($key<(sizeof($this->_breadcrumb)-1))

{

$output.=$this->_divider;

}

}

}

// we close the container's tag

$output.=$this->_container_close;

return$output;

}

}

Some changes to the prototype – move the configuration of the library in a separate file

Looks nice, but… There is room for improvement. Why not keeping the configuration of the library inside a config file? This way we can make sure no changes are made to the library.

So let’s create a make_bread.php file inside application/config directory. You might say “Why not inside application/config/development if we are working inside a development environment?”. My answer would be that no matter what the environment, the breadcrumb will look the same in the end.

The configuration file would have to store all the variables that can be found just before the constructor of the library. So it will look like this:

make_bread.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<?phpif(!defined('BASEPATH'))exit('No direct script access allowed');

// $config['include_home'] will tell the library if the first element should be the homepage. You only put the title of the first crumb. If you leave it blank it will not put homepage as first crumb

$config['include_home']='First page';

// $config['divider'] is the divider you want between the crumbs. Leave blank if you don't want a divider;

$config['divider']='&nbsp;&#8250;&nbsp;';

// $config['container_open'] is the opening tag of the breadcrumb container

$config['container_open']='<div id="breadcrumb">';

// $config['container_close'] is the closing tag of the breadcrumb container

$config['container_close']='</div>';

// $config['crumb_open'] is the opening tag of the crumb container

$config['crumb_open']='';

// $config['crumb_close'] is the closing tag of the crumb container

$config['crumb_close']='';

Now we will change the start of the Make_bread library so that it can fill the variables from the configuration file in the constructor:

Cool. Now, if we will need to make changes to the way the library works, we can make them inside the configuration file and leave the library alone.

The testing – Putting the library at work

Now we can test the library inside a controller. For starters, let’s do the test in the Welcome controller.

Welcome.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');

classWelcomeextendsCI_Controller{

publicfunctionindex()

{

// first load the library

$this->load->library('make_bread');

// add the first crumb, the segment being added to the previous crumb's URL

$this->make_bread->add('first crumb','testing',TRUE);

// add the second crumb, the segment not being added to the previous crumb's URL

$this->make_bread->add('second crumb','the_test',FALSE);

// add another crumb with a absolute URL

$this->make_bread->add('test','http://google.com');

// being the last crumb in the breadcrumb I want this to have no link, so I will only put the title

$this->make_bread->add('Testing breadcrumbs');

// now, let's store the output of the breadcrumb in a variable and show it (preferably inside a view)

$breadcrumb=$this->make_bread->output();

echo$breadcrumb;

}

}

As you can see, I’ve put an echo inside the controller, killing the MVC pattern philosophy. But at least we can testify that the library worked as a charm. You can download the whole library from https://github.com/avenirer/CI-breadcrumb-library.

10 thoughts on “Step 10 – Make your first library: a breadcrumb generator”

This is a very good tutorial and I really learn something. However I have a question about the breadcrumb library. How can I remove the anchor link to the last child of the breadcrumb? Is it possible also to create a breadcrumb that copies the current url and put links to every section?
Thanks again for this wonderful tutorial.

Thank you for your input. I actually did think about allowing the user not to add a link to the last crumb (for SEO and/or usability reasons), but when I wrote the tutorial I forgot about it. Now, thanks to you, I’ve changed the script so that, if you don’t put a link the crumb will show up without one.

That’s what I thought. But then I thought that, according to SEO and user friendly design principles, the active page should never be a link inside the breadcrumb. So you simply don’t link it and style it accordingly (hence without needing to add another class).