面包屑链接无法正常工作

发布于 2024-10-11 01:21:04 字数 956 浏览 3 评论 0 原文

好吧,我有这段代码 -

function breadcrumbs($separator = ' » ', $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>");
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
        $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";  
    }
    return implode($separator, $breadcrumbs);
}

假设我位于以下页面网址:

http://www.mysite.com/forums/general/log-book

此函数将面包屑正确显示为 首页 » 论坛 » 一般 » 日志

但是,当我单击其中一个面包屑链接时,它只会返回到基本站点 URL 结构 - 例如“常规”会返回到 http://www.mysite.com/general实际上应该去

http://www.mysite.com/forums/general

你们建议我如何解决这个问题?

Well I have this code -

function breadcrumbs($separator = ' » ', $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>");
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
        $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";  
    }
    return implode($separator, $breadcrumbs);
}

And lets say I am at the page url of:

http://www.mysite.com/forums/general/log-book

this function displays the breadcrumb correctly as Home » Forums » General » Log Book.

However, when I click on one of the breadcrumb links it only goes back to the basic site url structure - such as "General" goes back to http://www.mysite.com/general when it actually should go to

http://www.mysite.com/forums/general

How do you guys suggest I go about fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不忘初心 2024-10-18 01:21:04

请尝试以下操作:

function breadcrumbs($separator = ' » ', $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>");
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
        $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";  
        $base = $base.$crumb.'/';
    }
    return implode($separator, $breadcrumbs);
}

注意更改后的行 $base = $base.$crumb.'/';

Try this instead:

function breadcrumbs($separator = ' » ', $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>");
    foreach ($path AS $x => $crumb) {
        $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
        $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";  
        $base = $base.$crumb.'/';
    }
    return implode($separator, $breadcrumbs);
}

Note the changed line $base = $base.$crumb.'/';

糖果控 2024-10-18 01:21:04

鉴于您的 $path 数组如下所示:

array(
   'forums',
   'general',
   'log-book'
)

您可以向后遍历它:

while (NULL !== ($crumb = array_shift($path))) {
   $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
   $url = implode('/', $path) . '/' . $crumb;
   $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
}

注意 1:不要依赖 HTTP_HOST。请改用SERVER_NAME。前者可以使用 HTTP 请求的 Host: ... 字段进行伪造。

注 2:我是 Yoda 条件句的粉丝。

Given your $path array looks like:

array(
   'forums',
   'general',
   'log-book'
)

You might traverse it backwards:

while (NULL !== ($crumb = array_shift($path))) {
   $title = ucwords(str_replace(Array('.php', '_', '-'), Array('', ' ', ' '), $crumb));
   $url = implode('/', $path) . '/' . $crumb;
   $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
}

Note 1: Don't rely on HTTP_HOST. Use SERVER_NAME instead. The former can be forged using the HTTP request's Host: ... field.

Note 2: I'm a fan of Yoda conditionals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文