empecé con el código de Dominic Barnes, que se incorpora la retroalimentación de cWoDeR y todavía tenía problemas con los enlaces al tercer nivel cuando utilicé un subdirectorio. Así que lo reescribí y he incluido el código a continuación.
Tenga en cuenta que he levantado mi estructura del sitio web de tal manera que las páginas estén subordinadas a (vinculado a) una página en el nivel raíz están configurados de la siguiente manera:
Crear una carpeta con el EXACTA mismo nombre que el archivo (incluyendo capitalización), menos el sufijo, como una carpeta en el nivel raíz
lugar todos los archivos/páginas subordinadas a esta carpeta
(por ejemplo, si quieren páginas subalterno para clientes.php:
Esta estructura funcionará para múltiples niveles de subcarpetas.
Simplemente asegúrese de seguir la estructura de archivos descrita anteriormente E inserte un archivo index.php con el código que se muestra en cada subcarpeta.
El código en la página index.php en cada subcarpeta se parece a:
<!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>
Si utiliza el directorio raíz del servidor como su raíz web (es decir,/var/www/html) a continuación, establezca $ root_dir = "": (NO deje el final "/" en).Si utiliza un subdirectorio para su sitio web (es decir,/var/www/html/web_root, configure $ root_dir = "raíz_web /"; (reemplace web_root con el nombre real de su directorio web) (asegúrese de incluir el final /)
en todo caso, aquí está mi (derivado) código:
<?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>
?>
¿Alguien sabe cómo hacer esto en Twig? Consulte mi pregunta aquí: http://stackoverflow.com/questions/38551401/how-to-create-a-dynamic-breadcrumb-nav-in-twig – user3464091