php - 如果菜单位于不同的文件夹中,如何创建网站的 BASE URL?

标签 php html base-url

我知道这样做很愚蠢,但我遇到过在不同文件夹中创建菜单的情况。我试图为菜单创建 baseurl。

function getBaseUrl() 
{
     $currentPath = $_SERVER['PHP_SELF'];
    $pathInfo = pathinfo($currentPath);
    $hostName = $_SERVER['HTTP_HOST']; 
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';
    return $protocol.$hostName.$pathInfo['dirname']."/";
}

使用像 <?php echo getBaseUrl(); ?> 这样的基本 url .我的问题是:

让我有菜单aaa bbb ccc ddd。和页面 aaa菜单在里面 folder1/page1.php

bbb菜单在里面 folder2/page2.php .我不能从 aaa菜单到 bbb菜单。

有人有想法吗?请建议

最佳答案

您可以使用标签索引的页面列表创建一个数组:

function getBaseUrl($naviItem) 
{
    var $navigation = array( 
       'aaa_menu' => '/folder1/page1.php', 
       'bbb_menu' => '/folder2/page2.php'
    );

    //$currentPath = $_SERVER['PHP_SELF'];
    //$pathInfo = pathinfo($currentPath);
    $hostName = $_SERVER['HTTP_HOST']; 
    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https://'?'https://':'http://';

    // return the path relative to the passed navitem
    return $protocol.$hostName.$navigation[$naviItem];
}

然后在您的 HTML 中,您将这个标签传递给您想要实现 baseURL 的导航项:

<a href="<?php echo getBaseUrl('aaa_menu'); ?>">aaa</a>
<a href="<?php echo getBaseUrl('bbb_menu'); ?>">bbb</a>

关于php - 如果菜单位于不同的文件夹中,如何创建网站的 BASE URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36004291/

相关文章:

php - 从 Left Join 中的第一个表获取变量

html - fixed时背景图不覆盖,不固定时不垂直覆盖

html - 为什么导航不在标题的中心对齐?

php - 到底什么是 baseUrl

php - 为什么PHP不支持多线程?

php - 在 sql 查询中使用 SESSION 变量时尝试获取非对象的属性

html - 文本/标题中的内联列表

tomcat - Pentaho 仪表板不显示图形

javascript - RequireJS require() 无法解析同一子目录级别的文件名

PHP - array_map 比 foreach 快吗?