【问题标题】:CakePHP: How to make a dynamic navigation barCakePHP:如何制作动态导航栏
【发布时间】:2014-09-04 05:03:45
【问题描述】:

我的网站中有导航栏,例如:

这是我的代码:

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

在这张照片中,我正在使用 Staffs Controller。所以我的链接是“Home > Staffs”。如果我使用 Products Controller 链接将是 "Home > Products"

  1. 但是,如果我使用 Staffs Controller 并且操作是 Add。意思是“员工/添加”。我想在导航栏上显示它,例如 “Home > Staffs > Add”。那么我该怎么做呢?

  2. 如果我完成了#1。当前链接将是“主页 > 员工 > 添加”。当我想回到工作人员的索引时。我在“主页 > 员工 > 添加”中单击“员工”。我必须像

    一样在里面制作链接

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

使用 Staffs 控制器时,此链接正常。当我更改为 Product, Customer 时,它将被破坏。我怎样才能做到。

我正在使用 CakePHP 1.3 对不起,我问这样简单的事情。我是 Cake PHP 的新手。感谢您的帮助和阅读。

【问题讨论】:

    标签: php cakephp hyperlink cakephp-1.3 navigationbar


    【解决方案1】:

    您首先需要以结构化的方式解决这个问题。您需要收集要在面包屑中显示的所有链接的数组,然后遍历并输出所有链接。试试这样的:

    $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))
        );
    }
    

    现在您已经有了一个结构化数组,其中包含您要输出的三个链接,因此您可以像这样轻松地输出它们:

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

    注意:

    1. 您可能希望找到一种更好的方法来获取方法的标题,目前我只是将参数/控制器名称大写。
    2. 我没有对此进行测试,但理论上它应该可以工作。
    3. 可能有一个用于生成面包屑的内置助手 - 看看吧。
    4. 员工*(单数)

    【讨论】:

    • 感谢您的回答,但也许我还不够了解。我会对此进行研究。
    猜你喜欢
    • 1970-01-01
    • 2015-05-13
    • 2013-11-14
    • 2012-01-13
    • 1970-01-01
    • 2021-08-03
    • 2020-01-10
    • 1970-01-01
    • 2017-05-09
    相关资源
    最近更新 更多