使用PHP变量进行网站导航

使用PHP变量进行网站导航,php,if-statement,web,navigation,include,Php,If Statement,Web,Navigation,Include,我在一些网站上使用过这种方法,没有明显的问题。但我想知道,利用PHP变量作为网站导航/页面指示器,以便在1个文件(upper.PHP)中轻松管理站点范围内的更改,这是否是一种合适的方法?有什么缺点吗?可能是对服务器的请求太多、加载时间过长、编码不当或S.E.O.负面影响 为了简单起见,我省略了周围的代码,如等,并用“…”指示了更多代码的位置 index.php <?php $urhere="home"; include ("upper.php"); ?> <div>

我在一些网站上使用过这种方法,没有明显的问题。但我想知道,利用PHP变量作为网站导航/页面指示器,以便在1个文件(upper.PHP)中轻松管理站点范围内的更改,这是否是一种合适的方法?有什么缺点吗?可能是对服务器的请求太多、加载时间过长、编码不当或S.E.O.负面影响

为了简单起见,我省略了周围的代码,如等,并用“”指示了更多代码的位置

index.php

<?php $urhere="home"; include ("upper.php"); ?>
    <div>This is the Home page content</div>   
... 
<?php $urhere="about"; include ("upper.php"); ?>
    <div>This is the About page content</div>
...    
<?php $urhere="contact"; include ("upper.php"); ?>
    <div>This is the Contact page content</div> 
...
...

<meta name="description" content="
    <?php
        if ($urhere=="home") echo "Home page description";
        if ($urhere=="about") echo "About page description";
        if ($urhere=="contact") echo "Contact page description";
    ?>

...

<title>
    <?php
        if ($urhere=="home") echo "Home page Title";
        if ($urhere=="about") echo "About page Title";
        if ($urhere=="contact") echo "Contact page Title"; 
    ?>
</title>

...

<div id="nav">
    <ul>
      <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
      <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
      <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
    </ul>
</div>
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
    'home' => array(
        'url' => '/home',
        'linkText' => 'Home'
    ),
    'about' => array(
        'url' => '/about',
        'linkText' => 'About',
        'sublinks' => array(
            'who-we-are' => array(
                'url' => '/who-we-are',
                'linkText' => 'Who We Are'
            ),
            'what-we-do' => array(
                'url' => '/what-we-do',
                'linkText' => 'What We Do',
                'sublinks' => array(
                    'business' => array(
                        'url' => '/business',
                        'linkText' => 'Business'
                    ),
                    'technology' => array(
                        'url' => '/technology',
                        'linkText' => 'Technology'
                    )
                )
            )
        )
    ),
    'contact' => array(
        'url' => '/contact',
        'linkText' => 'Contact'
    )
);

function getLinkElement($pageType, array $linkData)
{
    $class = $urhere == $pageType ? 'urhere' : '';
    $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";

    if (isset($linkData['sublinks']))
    {
        $sublinkElements = array();

        foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
            $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);

        $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
    }
    else
        $sublinksListElement = '';

    return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
    <ul>
        <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
    </ul>
</div>
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
<?php
$titles = array(
    'home' => 'Home page Title',
    'about' => 'About page Title',
    'contact' => 'Contact page Title'
);

$descriptions = array(
    'home' => 'Home page description',
    'about' => 'About page description',
    'contact' => 'Contact page description'
);

$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>

...

<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>

...

<title><?php echo $titles[$urhere] ?></title>

...

<div id="nav">
    <ul>
        <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
        <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
        <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
    </ul>
</div>

这是主页内容
... 
about.php

<?php $urhere="home"; include ("upper.php"); ?>
    <div>This is the Home page content</div>   
... 
<?php $urhere="about"; include ("upper.php"); ?>
    <div>This is the About page content</div>
...    
<?php $urhere="contact"; include ("upper.php"); ?>
    <div>This is the Contact page content</div> 
...
...

<meta name="description" content="
    <?php
        if ($urhere=="home") echo "Home page description";
        if ($urhere=="about") echo "About page description";
        if ($urhere=="contact") echo "Contact page description";
    ?>

...

<title>
    <?php
        if ($urhere=="home") echo "Home page Title";
        if ($urhere=="about") echo "About page Title";
        if ($urhere=="contact") echo "Contact page Title"; 
    ?>
</title>

...

<div id="nav">
    <ul>
      <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
      <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
      <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
    </ul>
</div>
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
    'home' => array(
        'url' => '/home',
        'linkText' => 'Home'
    ),
    'about' => array(
        'url' => '/about',
        'linkText' => 'About',
        'sublinks' => array(
            'who-we-are' => array(
                'url' => '/who-we-are',
                'linkText' => 'Who We Are'
            ),
            'what-we-do' => array(
                'url' => '/what-we-do',
                'linkText' => 'What We Do',
                'sublinks' => array(
                    'business' => array(
                        'url' => '/business',
                        'linkText' => 'Business'
                    ),
                    'technology' => array(
                        'url' => '/technology',
                        'linkText' => 'Technology'
                    )
                )
            )
        )
    ),
    'contact' => array(
        'url' => '/contact',
        'linkText' => 'Contact'
    )
);

function getLinkElement($pageType, array $linkData)
{
    $class = $urhere == $pageType ? 'urhere' : '';
    $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";

    if (isset($linkData['sublinks']))
    {
        $sublinkElements = array();

        foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
            $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);

        $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
    }
    else
        $sublinksListElement = '';

    return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
    <ul>
        <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
    </ul>
</div>
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
<?php
$titles = array(
    'home' => 'Home page Title',
    'about' => 'About page Title',
    'contact' => 'Contact page Title'
);

$descriptions = array(
    'home' => 'Home page description',
    'about' => 'About page description',
    'contact' => 'Contact page description'
);

$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>

...

<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>

...

<title><?php echo $titles[$urhere] ?></title>

...

<div id="nav">
    <ul>
        <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
        <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
        <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
    </ul>
</div>

这是关于页面内容的详细信息
...    
contact.php

<?php $urhere="home"; include ("upper.php"); ?>
    <div>This is the Home page content</div>   
... 
<?php $urhere="about"; include ("upper.php"); ?>
    <div>This is the About page content</div>
...    
<?php $urhere="contact"; include ("upper.php"); ?>
    <div>This is the Contact page content</div> 
...
...

<meta name="description" content="
    <?php
        if ($urhere=="home") echo "Home page description";
        if ($urhere=="about") echo "About page description";
        if ($urhere=="contact") echo "Contact page description";
    ?>

...

<title>
    <?php
        if ($urhere=="home") echo "Home page Title";
        if ($urhere=="about") echo "About page Title";
        if ($urhere=="contact") echo "Contact page Title"; 
    ?>
</title>

...

<div id="nav">
    <ul>
      <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
      <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
      <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
    </ul>
</div>
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
    'home' => array(
        'url' => '/home',
        'linkText' => 'Home'
    ),
    'about' => array(
        'url' => '/about',
        'linkText' => 'About',
        'sublinks' => array(
            'who-we-are' => array(
                'url' => '/who-we-are',
                'linkText' => 'Who We Are'
            ),
            'what-we-do' => array(
                'url' => '/what-we-do',
                'linkText' => 'What We Do',
                'sublinks' => array(
                    'business' => array(
                        'url' => '/business',
                        'linkText' => 'Business'
                    ),
                    'technology' => array(
                        'url' => '/technology',
                        'linkText' => 'Technology'
                    )
                )
            )
        )
    ),
    'contact' => array(
        'url' => '/contact',
        'linkText' => 'Contact'
    )
);

function getLinkElement($pageType, array $linkData)
{
    $class = $urhere == $pageType ? 'urhere' : '';
    $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";

    if (isset($linkData['sublinks']))
    {
        $sublinkElements = array();

        foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
            $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);

        $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
    }
    else
        $sublinksListElement = '';

    return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
    <ul>
        <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
    </ul>
</div>
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
<?php
$titles = array(
    'home' => 'Home page Title',
    'about' => 'About page Title',
    'contact' => 'Contact page Title'
);

$descriptions = array(
    'home' => 'Home page description',
    'about' => 'About page description',
    'contact' => 'Contact page description'
);

$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>

...

<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>

...

<title><?php echo $titles[$urhere] ?></title>

...

<div id="nav">
    <ul>
        <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
        <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
        <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
    </ul>
</div>

这是联系人页面的内容
...
upper.php

<?php $urhere="home"; include ("upper.php"); ?>
    <div>This is the Home page content</div>   
... 
<?php $urhere="about"; include ("upper.php"); ?>
    <div>This is the About page content</div>
...    
<?php $urhere="contact"; include ("upper.php"); ?>
    <div>This is the Contact page content</div> 
...
...

<meta name="description" content="
    <?php
        if ($urhere=="home") echo "Home page description";
        if ($urhere=="about") echo "About page description";
        if ($urhere=="contact") echo "Contact page description";
    ?>

...

<title>
    <?php
        if ($urhere=="home") echo "Home page Title";
        if ($urhere=="about") echo "About page Title";
        if ($urhere=="contact") echo "Contact page Title"; 
    ?>
</title>

...

<div id="nav">
    <ul>
      <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
      <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
      <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
    </ul>
</div>
<meta name="description" content="<?php echo $description ?>"/>
...
<title><?php echo $title ?></title>
...
<?php
$pageTypeToLinkMap = array(
    'home' => array(
        'url' => '/home',
        'linkText' => 'Home'
    ),
    'about' => array(
        'url' => '/about',
        'linkText' => 'About',
        'sublinks' => array(
            'who-we-are' => array(
                'url' => '/who-we-are',
                'linkText' => 'Who We Are'
            ),
            'what-we-do' => array(
                'url' => '/what-we-do',
                'linkText' => 'What We Do',
                'sublinks' => array(
                    'business' => array(
                        'url' => '/business',
                        'linkText' => 'Business'
                    ),
                    'technology' => array(
                        'url' => '/technology',
                        'linkText' => 'Technology'
                    )
                )
            )
        )
    ),
    'contact' => array(
        'url' => '/contact',
        'linkText' => 'Contact'
    )
);

function getLinkElement($pageType, array $linkData)
{
    $class = $urhere == $pageType ? 'urhere' : '';
    $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";

    if (isset($linkData['sublinks']))
    {
        $sublinkElements = array();

        foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
            $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);

        $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
    }
    else
        $sublinksListElement = '';

    return "<li>$anchorElement$sublinksListElement</li>";
}
?>
<div id="nav">
    <ul>
        <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
    </ul>
</div>
<?php
$pageTitle = "Home";
$description = "This is home.";
$urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
?>
<div>This is the Home page content</div>
<?php
$pageTitle = "About";
$description = "About us";
$urhere = "about";
?>
<div>This is the About page content</div>
<?php
$titles = array(
    'home' => 'Home page Title',
    'about' => 'About page Title',
    'contact' => 'Contact page Title'
);

$descriptions = array(
    'home' => 'Home page description',
    'about' => 'About page description',
    'contact' => 'Contact page description'
);

$linkClasses = array_fill_keys(array_keys($titles), '');
$linkClasses[$urhere] = 'urhere';
?>

...

<meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>

...

<title><?php echo $titles[$urhere] ?></title>

...

<div id="nav">
    <ul>
        <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
        <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
        <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
    </ul>
</div>
。。。

构建网页没有对错之分,有很多选择。一般来说,我喜欢使用某种类型的模板系统,这使得页面的设计很容易从其背后的业务逻辑中分离出来

  • 你可以试试Smarty()之类的东西
  • 查看一些很棒的MVC PHP框架。下面的站点对MVC PHP框架进行了很好的比较

  • 构建网页没有正确或错误的方法,而且有很多选择。一般来说,我喜欢使用某种类型的模板系统,这使得页面的设计很容易从其背后的业务逻辑中分离出来

  • 你可以试试Smarty()之类的东西
  • 查看一些很棒的MVC PHP框架。下面的站点对MVC PHP框架进行了很好的比较

  • 我发现这种方法存在一些严重的维护问题

    每次添加新页面时,都必须添加三个(目前)if语句

    upper.php太了解其他页面——它们的窗口标题和元数据

    我建议不要使用
    $urhere
    变量在upper.php中做出所有决策,而是设置变量,如窗口标题和元数据,这些变量可以直接由upper.php使用。下面是一个例子:

    upper.php

    <?php $urhere="home"; include ("upper.php"); ?>
        <div>This is the Home page content</div>   
    ... 
    
    <?php $urhere="about"; include ("upper.php"); ?>
        <div>This is the About page content</div>
    ...    
    
    <?php $urhere="contact"; include ("upper.php"); ?>
        <div>This is the Contact page content</div> 
    ...
    
    ...
    
    <meta name="description" content="
        <?php
            if ($urhere=="home") echo "Home page description";
            if ($urhere=="about") echo "About page description";
            if ($urhere=="contact") echo "Contact page description";
        ?>
    
    ...
    
    <title>
        <?php
            if ($urhere=="home") echo "Home page Title";
            if ($urhere=="about") echo "About page Title";
            if ($urhere=="contact") echo "Contact page Title"; 
        ?>
    </title>
    
    ...
    
    <div id="nav">
        <ul>
          <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
          <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
          <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
        </ul>
    </div>
    
    <meta name="description" content="<?php echo $description ?>"/>
    ...
    <title><?php echo $title ?></title>
    ...
    <?php
    $pageTypeToLinkMap = array(
        'home' => array(
            'url' => '/home',
            'linkText' => 'Home'
        ),
        'about' => array(
            'url' => '/about',
            'linkText' => 'About',
            'sublinks' => array(
                'who-we-are' => array(
                    'url' => '/who-we-are',
                    'linkText' => 'Who We Are'
                ),
                'what-we-do' => array(
                    'url' => '/what-we-do',
                    'linkText' => 'What We Do',
                    'sublinks' => array(
                        'business' => array(
                            'url' => '/business',
                            'linkText' => 'Business'
                        ),
                        'technology' => array(
                            'url' => '/technology',
                            'linkText' => 'Technology'
                        )
                    )
                )
            )
        ),
        'contact' => array(
            'url' => '/contact',
            'linkText' => 'Contact'
        )
    );
    
    function getLinkElement($pageType, array $linkData)
    {
        $class = $urhere == $pageType ? 'urhere' : '';
        $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";
    
        if (isset($linkData['sublinks']))
        {
            $sublinkElements = array();
    
            foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
                $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
    
            $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
        }
        else
            $sublinksListElement = '';
    
        return "<li>$anchorElement$sublinksListElement</li>";
    }
    ?>
    <div id="nav">
        <ul>
            <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
        </ul>
    </div>
    
    <?php
    $pageTitle = "Home";
    $description = "This is home.";
    $urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
    ?>
    <div>This is the Home page content</div>
    
    <?php
    $pageTitle = "About";
    $description = "About us";
    $urhere = "about";
    ?>
    <div>This is the About page content</div>
    
    <?php
    $titles = array(
        'home' => 'Home page Title',
        'about' => 'About page Title',
        'contact' => 'Contact page Title'
    );
    
    $descriptions = array(
        'home' => 'Home page description',
        'about' => 'About page description',
        'contact' => 'Contact page description'
    );
    
    $linkClasses = array_fill_keys(array_keys($titles), '');
    $linkClasses[$urhere] = 'urhere';
    ?>
    
    ...
    
    <meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
    
    ...
    
    <title><?php echo $titles[$urhere] ?></title>
    
    ...
    
    <div id="nav">
        <ul>
            <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
            <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
            <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
        </ul>
    </div>
    

    我发现这种方法存在一些严重的维护问题

    每次添加新页面时,都必须添加三个(目前)if语句

    upper.php太了解其他页面——它们的窗口标题和元数据

    我建议不要使用
    $urhere
    变量在upper.php中做出所有决策,而是设置变量,如窗口标题和元数据,这些变量可以直接由upper.php使用。下面是一个例子:

    upper.php

    <?php $urhere="home"; include ("upper.php"); ?>
        <div>This is the Home page content</div>   
    ... 
    
    <?php $urhere="about"; include ("upper.php"); ?>
        <div>This is the About page content</div>
    ...    
    
    <?php $urhere="contact"; include ("upper.php"); ?>
        <div>This is the Contact page content</div> 
    ...
    
    ...
    
    <meta name="description" content="
        <?php
            if ($urhere=="home") echo "Home page description";
            if ($urhere=="about") echo "About page description";
            if ($urhere=="contact") echo "Contact page description";
        ?>
    
    ...
    
    <title>
        <?php
            if ($urhere=="home") echo "Home page Title";
            if ($urhere=="about") echo "About page Title";
            if ($urhere=="contact") echo "Contact page Title"; 
        ?>
    </title>
    
    ...
    
    <div id="nav">
        <ul>
          <li><a href="/home"<?php if ($urhere=="home") echo " class=\"urhere\""; ?>>Home</a></li>
          <li><a href="/about"<?php if ($urhere=="about") echo " class=\"urhere\""; ?>>About</a></li>
          <li><a href="/contact"<?php if ($urhere=="contact") echo " class=\"urhere\""; ?>>Contact</a></li>
        </ul>
    </div>
    
    <meta name="description" content="<?php echo $description ?>"/>
    ...
    <title><?php echo $title ?></title>
    ...
    <?php
    $pageTypeToLinkMap = array(
        'home' => array(
            'url' => '/home',
            'linkText' => 'Home'
        ),
        'about' => array(
            'url' => '/about',
            'linkText' => 'About',
            'sublinks' => array(
                'who-we-are' => array(
                    'url' => '/who-we-are',
                    'linkText' => 'Who We Are'
                ),
                'what-we-do' => array(
                    'url' => '/what-we-do',
                    'linkText' => 'What We Do',
                    'sublinks' => array(
                        'business' => array(
                            'url' => '/business',
                            'linkText' => 'Business'
                        ),
                        'technology' => array(
                            'url' => '/technology',
                            'linkText' => 'Technology'
                        )
                    )
                )
            )
        ),
        'contact' => array(
            'url' => '/contact',
            'linkText' => 'Contact'
        )
    );
    
    function getLinkElement($pageType, array $linkData)
    {
        $class = $urhere == $pageType ? 'urhere' : '';
        $anchorElement = "<a href=\"$linkData[url]\" class=\"$class\">$linkData[linkText]</a>";
    
        if (isset($linkData['sublinks']))
        {
            $sublinkElements = array();
    
            foreach ($linkData['sublinks'] as $sublinkPageType => $sublinkData)
                $sublinkElements[] = getLinkElement($sublinkPageType, $sublinkData);
    
            $sublinksListElement = '<ul>' . implode($sublinkElements) . '</ul>';
        }
        else
            $sublinksListElement = '';
    
        return "<li>$anchorElement$sublinksListElement</li>";
    }
    ?>
    <div id="nav">
        <ul>
            <?php foreach ($pageTypeToLinkMap as $pageType => $link) echo getLinkElement($pageType, array $link); ?>
        </ul>
    </div>
    
    <?php
    $pageTitle = "Home";
    $description = "This is home.";
    $urhere = "home"; // I guess you still need this variable to make decisions in the "nav" div.
    ?>
    <div>This is the Home page content</div>
    
    <?php
    $pageTitle = "About";
    $description = "About us";
    $urhere = "about";
    ?>
    <div>This is the About page content</div>
    
    <?php
    $titles = array(
        'home' => 'Home page Title',
        'about' => 'About page Title',
        'contact' => 'Contact page Title'
    );
    
    $descriptions = array(
        'home' => 'Home page description',
        'about' => 'About page description',
        'contact' => 'Contact page description'
    );
    
    $linkClasses = array_fill_keys(array_keys($titles), '');
    $linkClasses[$urhere] = 'urhere';
    ?>
    
    ...
    
    <meta name="description" content="<?php echo $descriptions[$urhere] ?>"/>
    
    ...
    
    <title><?php echo $titles[$urhere] ?></title>
    
    ...
    
    <div id="nav">
        <ul>
            <li><a href="/home" class="<?php echo $linkClasses['home'] ?>">Home</a></li>
            <li><a href="/about" class="<?php echo $linkClasses['about'] ?>">About</a></li>
            <li><a href="/contact" class="<?php echo $linkClasses['contact'] ?>">Contact</a></li>
        </ul>
    </div>
    

    非常有趣。因此,当添加新页面时,唯一需要添加的代码是$$pageTypeToLinkMap数组中的upper.php,对吗?nav ul构建本身?我看到的问题是,标题和描述必须在每个页面文件中编辑,而不是在1 upper.php文件中编辑。如果有一个带有子菜单的嵌套菜单呢?将标题和描述移动到更相关的文件中可以更好地分离职责。如果您认为文件之间切换是一个问题,为什么不将所有的应用程序代码放在一个文件中呢?我让你考虑一下。你能详细解释一下你所说的带子菜单的嵌套菜单是什么意思吗?我不同意责任的分离——页面包含所有内容,upper.php处理所有的内容和导航,因此它很干净,很容易快速编辑所有内容。所谓嵌套子菜单,我只是指嵌套在菜单中的子菜单,因此
    • 关于
      • 我们是谁
      • 我们做什么
    我更新了答案,用类属性修复了这个错误
    array\u keys($titles)
    返回
    $titles
    数组中所有键的数组
    array\u fill\u keys(array\u keys($titles),“”)
    返回一个数组,该数组的键与
    $titles
    数组的键相同,但每个键的值都是空字符串<代码>$linkClasses[$urhere]='urhere'
    “urhere”
    设置为等于
    $urhere
    的键的值。相关阅读:非常有趣。因此,当添加新页面时,唯一需要添加的代码是$$pageTypeToLinkMap数组中的upper.php,对吗?nav ul构建本身?我看到的问题是,标题和描述必须在每个页面文件中编辑,而不是在1 upper.php文件中编辑。如果有一个带有子菜单的嵌套菜单呢?将标题和描述移动到更相关的文件中可以更好地分离职责。如果您认为文件之间切换是一个问题,为什么不将所有的应用程序代码放在一个文件中呢?我让你考虑一下。你能详细解释一下你所说的带子菜单的嵌套菜单是什么意思吗?我不同意责任的分离——页面包含所有内容,upper.php处理所有的内容和导航,因此它很干净,很容易快速编辑所有内容。所谓嵌套子菜单,我只是指嵌套在菜单中的子菜单,因此
    • 关于
      • 我们是谁
      • 我们做什么
    我更新了答案,用类属性修复了这个错误
    array\u keys($titles)
    返回
    $titles
    数组中所有键的数组
    array\u fill\u keys(array\u keys($titles),“”)
    返回一个数组,该数组的键与
    $titles
    数组的键相同,但每个键的值都是空字符串<代码>$linkClasses[$urhere]='urhere'
    “urhere”
    设置为等于
    $urhere
    的键的值。相关阅读: