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
552 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