CodeIgniter Forums
Breadcrumb helper or breadcrumb mode? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Breadcrumb helper or breadcrumb mode? (/showthread.php?tid=55832)



Breadcrumb helper or breadcrumb mode? - El Forum - 11-12-2012

[eluser]Lpeek[/eluser]
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!


Breadcrumb helper or breadcrumb mode? - El Forum - 11-12-2012

[eluser]JoostV[/eluser]
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.
Code:
// 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));

Code:
// 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;
}
Code:
// View file: all you have to do here is echo helper function and pass in breadcrumb array
echo wrap_breadcrumb ($breadcrumbs);



Breadcrumb helper or breadcrumb mode? - El Forum - 06-13-2014

[eluser]Lykos22[/eluser]
[quote author="JoostV" date="1352759693"]
Code:
// 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));
[/quote]

@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??


Breadcrumb helper or breadcrumb mode? - El Forum - 06-16-2014

[eluser]JoostV[/eluser]
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
Code:
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
Code:
$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/', 'Home'));
Because the add method returns the library itself you can also use chaining:
Code:
$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/', 'Home'))->add(anchor('about-us', 'About Us'));



Breadcrumb helper or breadcrumb mode? - El Forum - 06-16-2014

[eluser]Lykos22[/eluser]
[quote author="JoostV" date="1402910833"]
If you generate breadcrumbs based on data from multiple tables (such as posts, or pages) then I would use a library. Something like
Code:
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
Code:
$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/', 'Home'));
Because the add method returns the library itself you can also use chaining:
Code:
$this->load->library('breadcrumbs');
$this->breadcrumbs->add(anchor('/', 'Home'))->add(anchor('about-us', 'About Us'));
[/quote]

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

Code:
<ol class="breadcrumb">
  <li><a href="#">Home</a></li>
  <li><a href="#">Library</a></li>
  <li class="active">Data</li>
</ol>



Breadcrumb helper or breadcrumb mode? - El Forum - 06-16-2014

[eluser]JoostV[/eluser]
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)
Code:
$breadcrumbs = $this->breadcrumbs->getBreadcrumbs();
if (count($breadcrumbs)) {
    echo ol($breadcrumbs, array('class' => 'breadcrumb'));
}