我開始與多米尼克·巴恩斯的代碼,從cWoDeR納入反饋,我仍然不得不用麪包屑問題在第三級的時候我用了一個子目錄。所以我重寫了它,並且包含了下面的代碼。
請注意,我已經設置了我的網站結構,使得頁面從屬於(從鏈接)一個在根目錄一級頁面被設置如下:
(例如,如果想爲Customers.php sobordinate頁:
此結構將適用於多個級別的子文件夾。
只要確保你遵循上述文件結構,並插入一個index.php文件與每個子文件夾中顯示的代碼。
在每個子文件夾中的index.php頁面的代碼如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Redirected</title>
</head>
<body>
<?php
$root_dir = "web_root/" ;
$last_dir=array_slice(array_filter(explode('/',$_SERVER['PHP_SELF'])),-2,1,false) ;
$path_to_redirect = "/".$root_dir.$last_dir[0].".php" ;
header('Location: '.$path_to_redirect) ;
?>
</body>
</html>
如果您使用的服務器的根目錄作爲Web根目錄(即在/ var/www/html等),然後設置$ root_dir =「」:(不要在後面留下「/」)。如果你爲你的網站使用了一個子目錄(例如/ var/www/html/web_root,那麼設置$ root_dir =「web_root /」;(用web目錄的實際名稱替換web_root)(確保包含尾部/)
無論如何,這是我的(衍生)代碼:
<?php
// Big Thank You to the folks on StackOverflow
// See http://stackoverflow.com/questions/2594211/php-simple-dynamic-breadcrumb
// Edited to enable using subdirectories to /var/www/html as root
// eg, using /var/www/html/<this folder> as the root directory for this web site
// To enable this, enter the name of the subdirectory being used as web root
// in the $directory2 variable below
// Make sure to include the trailing "/" at the end of the directory name
// eg use $directory2="this_folder/" ;
// do NOT use $directory2="this_folder" ;
// If you actually ARE using /var/www/html as the root directory,
// just set $directory2 = "" (blank)
// with NO trailing "/"
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' » ' , $home = 'Home')
{
// This sets the subdirectory as web_root (If you want to use a subdirectory)
// If you do not use a web_root subdirectory, set $directory2=""; (NO trailing /)
$directory2 = "web_root/" ;
// This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ;
$path_array = array_filter(explode('/',$path)) ;
// This line of code accommodates using a subfolder (/var/www/html/<this folder>) as root
// This removes the first item in the array path so it doesn't repeat
if ($directory2 != "")
{
array_shift($path_array) ;
}
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'. $directory2 ;
// Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
$breadcrumbs = Array("<a href=\"$base\">$home</a>") ;
// Get the index for the last value in our path array
$last = end($path_array) ;
// Initialize the counter
$crumb_counter = 2 ;
// Build the rest of the breadcrumbs
foreach ($path_array as $crumb)
{
// Our "title" is the text that will be displayed representing the filename without the .suffix
// If there is no "." in the crumb, it is a directory
if (strpos($crumb,".") == false)
{
$title = $crumb ;
}
else
{
$title = substr($crumb,0,strpos($crumb,".")) ;
}
// If we are not on the last index, then create a hyperlink
if ($crumb != $last)
{
$calling_page_array = array_slice(array_values(array_filter(explode('/',$path))),0,$crumb_counter,false) ;
$calling_page_path = "/".implode('/',$calling_page_array).".php" ;
$breadcrumbs[] = "<a href=".$calling_page_path.">".$title."</a>" ;
}
// Otherwise, just display the title
else
{
$breadcrumbs[] = $title ;
}
$crumb_counter = $crumb_counter + 1 ;
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs) ;
}
// <p><?= breadcrumbs() ? ></p>
// <p><?= breadcrumbs(' > ') ? ></p>
// <p><?= breadcrumbs(' ^^ ', 'Index') ? ></p>
?>
任何人都知道如何在Twig中做到這一點? 在這裏看到我的問題:http://stackoverflow.com/questions/38551401/how-to-create-a-dynamic-breadcrumb-nav-in-twig – user3464091 2016-07-25 07:49:57