我有这段代码 -
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
此函数将面包屑正确显示为Home » Forums » General » Log Book
。
然而,当我点击其中一个痕迹导航链接时,它只会回到基本网站网址结构 - 例如“常规”会回到http://www.mysite.com/general
,当它实际应该转到
http://www.mysite.com/forums/general
你们怎么建议我去解决这个问题?
答案 0 :(得分:2)
请改为尝试:
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.'/';
答案 1 :(得分:1)
鉴于您的$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:我是尤达条件的粉丝。