EllisLab text mark
Advanced Search
     
Breadcrumb helper or breadcrumb mode?
Posted: 12 November 2012 02:25 AM
Joined: 2012-10-11
18 posts

Hey! I’m just putting together some code to build breadcrumbs, I’m just wondering, I would have initially thought to create it as a helper, but as it will be interacting with a database should it actually be a model?

Or maybe neither of those are correct and it should actually be a library?

What would you usually do?

Cheers!

 
Posted: 12 November 2012 05:34 PM   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
555 posts

What I usually do, as a rule of thumb: everything that is business logic goes inside a model. Helpers are usually good for reusable view logic. So, when you’re retrieving breadcrumb data, do it in a model and pass it to a view. If you need to wrap breadcrumb data into HTML use a helper.

// Controller: fetch breadcrumbs. Typically, this would return an array of objects or something
$breadcrumbs $this->breadcrumb_m->get();
$this->load->view('some_view', array('breadcrumbs' => $breadcrumbs)); 
// Helper
/**
 * Wrap breadcrumbs in HTML and return it
 * @param array $breadcrumbs
 * @return string
 */
function wrap_breadcrumb ($breadcrumbs)
{
 
if (! is_array($breadcrumbs) || ! count($breadcrumbs)) return '';
 
 
$retval '<ul>' PHP_EOL;
 foreach (
$breadcrumbs as $breadcrumb)
 
{
  $retval 
.= '<li>' anchor($breadcrumb->url$breadcrumb->title) . '</li>' PHP_EOL;
 
}
 $retval 
.= '</ul>' PHP_EOL;
// View file: all you have to do here is echo helper function and pass in breadcrumb array
echo wrap_breadcrumb ($breadcrumbs); 
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model

 
Posted: 13 June 2014 08:25 AM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2013-05-12
62 posts
JoostV - 12 November 2012 05:34 PM
// Controller: fetch breadcrumbs. Typically, this would return an array of objects or something
$breadcrumbs $this->breadcrumb_m->get();
$this->load->view('some_view', array('breadcrumbs' => $breadcrumbs)); 

@JoostV: This breadcrumb_m , is this a model you have created your own, or a model based on a third-part library ?

I don’t get it.. if it is a model you have created, how do you specify the name of the table and the fields you want to get in order to generate breadcrumbs??

 
Posted: 16 June 2014 05:27 AM   [ # 3 ]   [ Rating: 1 ]
Avatar
Joined: 2008-01-07
555 posts

This is just pseudo code. I personally don’t store breadcrumbs in a database, but you said “as it will be interacting with a database”

If you do store breadcrumbs in their own table, then a model would be the way to go.
If you generate breadcrumbs based on data from multiple tables (such as posts, or pages) then I would use a library. Something like

class breadcrumbs
{
    
protected $breadcrumbs = array();

    public function 
getBreadcrumbs ()
    
{
        
return $this->breadcrumbs;
    
}

    
public function add ($breadcrumb)
    
{
        $this
->breadcrumbs[] $breadcrumb;
        return 
$this;
    
}

This way you could do stuff like

$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/''Home')); 

Because the add method returns the library itself you can also use chaining:

$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/''Home'))->add(anchor('about-us''About Us')); 
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model

 
Posted: 16 June 2014 07:13 AM   [ # 4 ]   [ Rating: 0 ]
Avatar
Joined: 2013-05-12
62 posts
JoostV - 16 June 2014 05:27 AM

If you generate breadcrumbs based on data from multiple tables (such as posts, or pages) then I would use a library. Something like

class breadcrumbs
{
    
protected $breadcrumbs = array();

    public function 
getBreadcrumbs ()
    
{
        
return $this->breadcrumbs;
    
}

    
public function add ($breadcrumb)
    
{
        $this
->breadcrumbs[] $breadcrumb;
        return 
$this;
    
}

This way you could do stuff like

$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/''Home')); 

Because the add method returns the library itself you can also use chaining:

$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/''Home'))->add(anchor('about-us''About Us')); 

Yes that’s what I was trying to say breadcrumbs based on data from multiple tables (such as posts, or pages)

So basicly this library does not interact with the database on its own, you set the breadcrumbs manualy on your controller or view, right? and how do you set the html layout then? what I mean is how do you generate this

<ol class="breadcrumb">
  <
li><a href="#">Home</a></li>
  <
li><a href="#">Library</a></li>
  <
li class="active">Data</li>
</
ol
 
Posted: 16 June 2014 09:44 AM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-07
555 posts

Right, you add breadcrumbs in the controller and such. You can add them to your view like so (assuming you loaded the breadcrumbs library and html helper)

$breadcrumbs $this->breadcrumbs->getBreadcrumbs();
if (
count($breadcrumbs)) {
    
echo ol($breadcrumbs, array('class' => 'breadcrumb'));
 Signature 

View my Tutsplus course ‘Build a CMS with Codeigniter’
http://codeigniter.tv/ - Codeigniter video tutorials
Video tutorial series on MY_Model