动态面包屑没有看到嵌套文件夹

时间:2013-01-01 23:42:18

标签: php dynamic breadcrumbs

我将代码here用于我网站的动态php面包屑。它很棒!但是,如果我将一个文件夹嵌套超过1深,则会导致错误。

这是我目前用于面包屑的代码。

    <?php
    function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
        $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
        $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
        $breadcrumbs = Array("<a href=\"$base\">$home</a>");
        $last = end(array_keys($path));
        foreach ($path AS $x => $crumb) {
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
            else
                $breadcrumbs[] = $title;
        }
        return implode($separator, $breadcrumbs);
     }
    ?>
    You are here: <?= breadcrumbs(' ♥ ') ?>

查看实例的最简单的地方是here。如果单击痕迹导轨上的第三个链接,它将忽略第二个嵌套文件夹。我不知道足够的PHP来解决问题以及如何解决它。我认为理想情况下会在网址中查看嵌套文件夹。

3 个答案:

答案 0 :(得分:0)

您需要添加以前的碎屑才能使其正常工作。这是一个修复:

<?php
function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
    $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");
    $last = end(array_keys($path));
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
        if ($x != $last)
            $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
        else
            $breadcrumbs[] = $title;
        $base .= $crumb . '/';
    }
    return implode($separator, $breadcrumbs);
 }
?>
You are here: <?= breadcrumbs(' ♥ ') ?>

答案 1 :(得分:0)

那是因为你只是链接到当前的“crumb”$base$crumb

因为你需要在构建链接时跟踪路径

foreach ($path AS $x => $crumb) {
            $base .= $crumb.'/';   // <- keep adding crumbs to current path
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"$base\">$title</a>";  //<- link to current path
            else
                $breadcrumbs[] = $title;
        }

答案 2 :(得分:0)

假设你的脚本运行正常,我看到你有open_basedir restriction in effect所以如果你使用apache将它添加到你的httpd.conf

<Directory /var/www/vhosts/domain.tld/httpdocs>
 php_admin_value open_basedir none
 </Directory>

如果脚本导致问题,请尝试其中一个答案