TommyDo
TommyDo

Reputation: 673

CakePHP: How to make a dynamic navigation bar

I have the Navigation Bar in my website like:

enter image description here

Here my code to make it:

        <ul class="breadcrumb">
            <li>
                <i class="icon-home"></i>
                <a href="index.html">Home</a> 
                <i class="icon-angle-right"></i>
            </li>
            <li>
            <?php echo $this->Html->link($title_for_layout,array('controller'=>'controllers','action'=>'index','full_base'=>true));?>
            </li>
        </ul>

In this picture, I am working with Staffs Controller. So my link is "Home > Staffs". If I do with Products Controller link will be "Home > Products".

  1. But if I work with Staffs Controller and Action is Add. Means "Staffs/add". I want show it on navigation bar like "Home > Staffs > Add". So How can I do this?

  2. If I have done the #1. The current link will be "Home > Staffs > Add". When I want to back to the index of Staffs. I click to "Staffs" in "Home > Staffs > Add". I have to make the link inside like

    $this->Html->link($title_for_layout,array('controller'=>'staffs','action'=>'index')

This link is ok when working with Staffs controller. When I change to Product, Customer it will be broken. How can I make it.

I am using CakePHP 1.3 Sorry I ask simple things like this. I am new in Cake PHP. Thank for help and reading.

Upvotes: 0

Views: 1272

Answers (1)

scrowler
scrowler

Reputation: 24435

You sort of need to address this in a structured way to start with. You need to gather an array of all the links you're going to display in your breadcrumbs, then go through and output it all. Try something like this:

$links = array();
// Add the home URL
$links[] = array(
    'icon' => 'icon-home',
    'title' => 'Home',
    'url' => 'index.html'
);

// Add the controller
$links[] = array(
    'title' => ucwords($this->params['controller']),
    'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => 'index', 'full_base' => true))
);

// Now, conditionally add the next parts where necessary

$param1 = isset($this->params['pass'][0]) ? $this->params['pass'][0] : null;
if($param1) {
    $links[] = array(
        'title' => ucwords($param1),
        'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => $this->action, $param1))
    );
}

Now you've got a structured array containing the three links you're going to output, so you can output them easily like this:

<ul class="breadcrumb">
    <?php
    $size = count($links);
    foreach($links as $i => $link) : ?>
        <li>
            <?php
            // Output icon if it's set
            if(isset($link['icon']))
                echo '<i class="' . $link['icon'] . '"></i>'; ?>

            // Output the link itself
            echo $this->Html->link($link['title'], $link['url']);

            // Output the caret if necessary (it's not the last)
            if($i < $size - 1)
                echo '<i class="icon-angle-right"></i>';
            ?>
        </li>
    <?php endforeach; ?>
</ul>

Note:

  1. You might want to find a better way to get the title for your method, at the moment I'm just capitalizing the parameter/controller name.
  2. I haven't tested this, but it should work in theory.
  3. There's probably an inbuilt helper for generating breadcrumbs - have a look.
  4. staff* (singular)

Upvotes: 1

Related Questions