【问题标题】:Using PHP variables for website navigation使用 PHP 变量进行网站导航
【发布时间】:2013-09-06 22:31:00
【问题描述】:

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

为了简单起见,我省略了周围的代码,例如

等,并用“...”表示更多代码的位置

index.php

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

about.php

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

contact.php

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

upper.php

...

<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>

【问题讨论】:

    标签: php if-statement web navigation include


    【解决方案1】:

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

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

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

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

    upper.php

    <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>
    

    index.php

    <?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>
    

    about.php

    <?php
    $pageTitle = "About";
    $description = "About us";
    $urhere = "about";
    ?>
    <div>This is the About page content</div>
    

    编辑

    作为对your comment 的回应,以下是在保持原始结构不变的情况下改进upper.php 的方法:

    upper.php

    <?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-less 代码。

    【讨论】:

    • 非常有趣。因此,当添加新页面时,唯一需要添加的代码是在 $$pageTypeToLinkMap 数组中的 upper.php 中,对吗? nav ul 自己构建?
    • 我看到的问题是标题和描述必须在每个页面文件中进行编辑,而不是全部在 1 upper.php 文件中。如果有一个带有子菜单的嵌套菜单呢?
    • 将标题和描述移动到更相关的文件中可以更好地分离职责。如果您认为在文件之间切换是一个问题,为什么不将所有应用程序代码放在一个文件中呢?我会让你考虑一下。您能否详细说明嵌套菜单和子菜单的含义?
    • 我不同意职责分离 - 页面包含所有内容,upper.php 处理所有 和导航,因此快速编辑所有内容既干净又容易。嵌套子菜单是指嵌套在菜单中的子菜单,所以&lt;ul&gt;&lt;li&gt;About&lt;ul&gt;&lt;li&gt;Who We Are&lt;/li&gt;&lt;li&gt;What We Do&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;
    • 我更新了答案以使用类属性修复该错误。 array_keys($titles) 返回 $titles 数组中所有键的数组。 array_fill_keys(array_keys($titles), '') 返回一个数组,它与$titles 数组具有相同的键,但每个键的值是一个空字符串。 $linkClasses[$urhere] = 'urhere';"urhere" 设置为等于$urhere 的键的值。相关阅读:php.net/manual/en/language.types.array.phpde1.php.net/manual/en/function.array-keys.phpde1.php.net/manual/en/function.array-fill-keys.php
    【解决方案2】:

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

    1. 您可以尝试 Smarty (http://www.smarty.net/crash_course) 之类的方法。
    2. 查看一些很棒的 MVC PHP 框架。在下面的站点上有一个很好的 MVC PHP 框架比较

    http://jonathanmh.com/best-php-mvc-frameworks-of-2013/

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多