doutang3815 2019-05-15 01:25
浏览 54
已采纳

如何制作URL的特定部分的痕迹

I have to generate breadcrumbs for each page of website, where as the url is bit complex.

This is my URL

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

Now I have to get "enterprise/projects/manage" from the url after the "route=" and generate breadcrumbs of each route.

Below is the PHP code that I am using, which only creates breadcrumb like this "Home>>Company " whereas I need breadcrumb like this "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);
    } 
    ?> `
  • 写回答

1条回答 默认 最新

  • douxuan0698 2019-05-15 02:51
    关注

    Since you only want the route parameter, then you need to mine that out of the url.

    Here is a function to extract only the route parameter.

    function getRouteParamAsArray($url) {
        // Get the query part of the url. i.e. after the ?
        $query = parse_url($url, PHP_URL_QUERY);
        // echo "query = $query
    ";
        // explode it into parts a=1, b=2 etc
        $parts = explode('&', $query);
    
        // echo print_r($parts, true) . "
    ";
    
        // 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
    ";
            $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) . "
    ";
        // See if there is a 'route' key
        if ( array_key_exists('route', $arr) ) {
            $routeParts = explode('/', $arr['route']);
        } else {
            $routeParts = [];
        }
        // echo print_r($routeParts, true) . "
    ";
        // echo 'breadcrumb: ' . implode(' > ', $routeParts) . "
    ";
        return $routeParts;
    }
    

    Integrate it into your function:

    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);
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式