【问题标题】:How to make breadcrumb of specific part of URL如何制作 URL 特定部分的面包屑
【发布时间】:2019-05-15 01:25:38
【问题描述】:

我必须为网站的每个页面生成面包屑,因为网址有点复杂。

这是我的网址

http://1.1.1.1/Company/?route=enterprise/projects/manage&id=52rw9649ffwerwd3d9018154&section=newpanel

现在我必须从“route=”之后的 url 中获取“enterprise/projects/manage”并生成每个路由的面包屑。

下面是我正在使用的 PHP 代码,它只创建像“Home>>Company”这样的面包屑,而我需要像“enterprise>>projects>>manage”这样的面包屑。

 // This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path

    function breadcrumbs($separator = ' » ', $home = 'Home') {
        // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
        $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));

        // This will build our "base URL" ... Also accounts for HTTPS :)
        $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

        // 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>");

        // Find out the index for the last value in our path array
        $last = end(array_keys($path));

        // Build the rest of the breadcrumbs
        foreach ($path AS $x => $crumb) {
            // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));

            // If we are not on the last index, then display an <a> tag
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
            // Otherwise, just display the title (minus)
            else
                $breadcrumbs[] = $title;
        }

        // Build our temporary array (pieces of bread) into one big string :)

        return implode($separator, $breadcrumbs);
        //return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    } 
    ?> `

【问题讨论】:

    标签: php breadcrumbs


    【解决方案1】:

    既然你只想要路由参数,那么你需要从 url 中挖掘出来。

    这是一个只提取路由参数的函数。

    function getRouteParamAsArray($url) {
        // Get the query part of the url. i.e. after the ?
        $query = parse_url($url, PHP_URL_QUERY);
        // echo "query = $query\n";
        // explode it into parts a=1, b=2 etc
        $parts = explode('&', $query);
    
        // echo print_r($parts, true) . "\n";
    
        // Now covert the parts into key value pairs
        // by taking each item and exploding on '='
        $arr = [];
        array_walk($parts, function($item) use (&$arr)
        {
            // echo "$item\n";
            $a = explode('=', $item, 2); // convert into key/value
            $key = $a[0];
            $value = $a[1] ?? null; // just in case
            $arr[$key] = $value;
        });
        // Now we have key value pairs
        //echo print_r($arr, true) . "\n";
        // See if there is a 'route' key
        if ( array_key_exists('route', $arr) ) {
            $routeParts = explode('/', $arr['route']);
        } else {
            $routeParts = [];
        }
        // echo print_r($routeParts, true) . "\n";
        // echo 'breadcrumb: ' . implode(' > ', $routeParts) . "\n";
        return $routeParts;
    }
    

    将其集成到您的函数中:

    function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
        // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
        $path = getRouteParamAsArray($_SERVER['REQUEST_URI']);
    
        // This will build our "base URL" ... Also accounts for HTTPS :)
        $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
    
        // 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}Company/">$home</a>");
    
        // Find out the index for the last value in our path array
        $last = end(array_keys($path));
    
        // Build the rest of the breadcrumbs
        $crumbPath = '';
        foreach ($path AS $x => $crumb) {
            // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
            $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
            if ( ! empty($crumbPath) ) {
                $crumbPath .= '/';
            }
            $crumbPath .= $crumb;
            // If we are not on the last index, then display an <a> tag
            if ($x != $last)
                $breadcrumbs[] = "<a href=\"{$base}Company/?route={$crumbPath}\">$title</a>";
            // Otherwise, just display the title (minus)
            else
                $breadcrumbs[] = $title;
        }
    
        // Build our temporary array (pieces of bread) into one big string :)
    
        return implode($separator, $breadcrumbs);
        //return parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
    } 
    

    【讨论】:

    • 非常感谢@ryantxr 的回复,它确实有效,我确实得到了这个面包屑 enterprise>>projects>>manage 但现在如果我点击项目,而不是带我去 1.1.1.1/Company/?route=enterprise/projects ,它带我去 1.1.1.1/projects
    • 看起来你的原始代码应该在创建路由屑时连接它们。
    • 我认为是这部分代码if ($x != $last) $breadcrumbs[] = "&lt;a href=\"$base$crumb\"&gt;$title&lt;/a&gt;"; // Otherwise, just display the title (minus) else $breadcrumbs[] = $title; }@ryantxr
    • 你能看看我的代码吗,我真的被这个问题困住了。 @ryantxr
    • 只需更改生成的字符串即可。我更新了代码。您应该检查它并确认它有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多