创建面包屑的最佳方法是什么?

时间:2019-08-06 09:43:19

标签: php url breadcrumbs

一段时间以来,我一直在搜索并尝试在我的网站上制作面包屑。但是由于某种原因,我仍然失败。 我用来创建它的代码:

<?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[] = urldecode($title);
    }

    return implode($separator, $breadcrumbs);
}

?>


<?php
echo breadcrumbs();
?>

我得到的结果:

Home » Mandraborino » Product » 1 » извара.html

此产品的链接是:

http://localhost/mandraborino/product/1/%D0%B8%D0%B7%D0%B2%D0%B0%D1%80%D0%B0.html

我使用.htaccess来创建友好的网址

RewriteRule ^product/(.*?)/(.*?)/(.*?).html$ product.php?id=$1&name=$2&lang=$3 [L]
RewriteRule ^product/(.*?)/(.*?).html$ product.php?id=$1&name=$2 [L]

0 个答案:

没有答案