代码之家  ›  专栏  ›  技术社区  ›  ItsPronounced Finn

PHP包含最佳实践问题

  •  23
  • ItsPronounced Finn  · 技术社区  · 14 年前

    我一直在学习PHP的语法并进行实践。我来自.NET的背景,所以母版页在页眉和页脚方面对我来说总是很容易。

    到目前为止我有一个mainHeader.php文件以及mainFooter.php其中有我的头菜单和我的页脚html。我创造了一个主体.php我把它放在最上面

    <?php include "mainHeader.php" ?>
    

    对于我放的页脚

    <?php include "mainFooter.php" ?>
    

    这很好地工作,让我微笑,因为我的网页都很好地结合在一起。主标题有我的 <html> <body> 我的主页脚有我的结束标签。

    这种做法好吗?

    9 回复  |  直到 14 年前
        1
  •  51
  •   ItsPronounced Finn    6 年前

    我包括来自控制器的视图。我还定义了文件位置,以便于维护。

    配置.php

    define('DIR_BASE',      dirname( dirname( __FILE__ ) ) . '/');
    define('DIR_SYSTEM',    DIR_BASE . 'system/');
    define('DIR_VIEWS',     DIR_SYSTEM . 'views/');
    define('DIR_CTLS',      DIR_SYSTEM . 'ctls/');
    define('DIR_MDLS',      DIR_SYSTEM . 'mdls/');
    define('VIEW_HEADER',   DIR_VIEWS . 'header.php');
    define('VIEW_NAVIGATION',   DIR_VIEWS . 'navigation.php');
    define('VIEW_FOOTER',   DIR_VIEWS . 'footer.php');
    

    config.php

    控制器.php

    require( '../config.php' );
    include( DIR_MDLS . 'model.php' );
    
    $model = new model();
    if ( $model->getStuff() ) {
        $page_to_load = DIR_VIEWS . 'page.php';
    }
    else {
        $page_to_load = DIR_VIEWS . 'otherpage.php';
    }
    
    include( VIEW_HEADER );
    include( VIEW_NAVIGATION );
    include( DIR_VIEWS . $page_to_load );
    include( VIEW_FOOTER );
    
        2
  •  5
  •   BalusC    14 年前

    你也可以反过来做。有一个带有页眉/页脚的主页,只包含正文。

    <!DOCTYPE html>
    <html lang="en">
        <head>
            ...
        </head>
        <body>
            <?php include $page ?>
        </body>
    </html>
    
        3
  •  3
  •   Xeoncross    14 年前

    在开始使用“视图”或“模板”之前,您所做的一切都是正常的,在这种情况下,您将不再在运行的“控制器”或“操作”中安排内容HTML。

    相反,您将加载一个视图,并用值填充它,从而将所有HTML源代码的顺序留给视图,而不是PHP文件。

    $view = new View('layout.php');
    $view->header = $header;
    $view->content = 'This is the main content!';
    $view->footer = $footer;
    print $view;
    

    然后加载布局文件,如下所示:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            ...
        </head>
        <body>
            <div id="header"><?php print $header; ?></div>
            <div id="content"><?php print $content; ?></div>
            <div id="footer"><?php print $footer; ?></div>
        </body>
    </html>
    
        4
  •  3
  •   Your Common Sense    14 年前


    部分基于Galen和Balus的观点:

    page.php

    require $_SERVER['DOCUMENT_ROOT'].'/../config.php';
    $data = get_data(); // assume we get all required data here.
    $pagetitle = "This is a sample page";
    $template = "page.tpl.php";
    include "main.tpl.php";
    

    main.tpl.php

    <!DOCTYPE html> 
    <html lang="en"> 
        <head> 
             <title><?php echo $pagetitle?></title>
        </head> 
        <body> 
            <?php include $template ?> 
        </body> 
    </html> 
    

    page.tpl.php

    <h1><?php echo $pagetitle?></h1>
    <?php if (!$data): ?>
    No news yet :-(
    <?php else: ?>
    <ul>
    <? foreach ($data as $row): ?>
    <li><a href="news.php?id=<?php echo $row['name']?>"><?php echo $row['name']?></a></li>
    <?php endforeach ?>
    </ul>
    <?php endif ?>
    
        5
  •  2
  •   aularon    14 年前

    smarty . 对于整个应用程序,请考虑使用 framework codeigniter .

        6
  •  1
  •   siliconrockstar    12 年前

        7
  •  1
  •   Daniel W.    4 年前

    公认的答案是从2010年开始,在过去的十年里情况发生了变化。

    该走的路,现在就走 composer 已经取代了大多数手有线自动装弹机,最佳做法是使用一个单一的 require_once 利用 __DIR__

    require_once __DIR__ . '/vendor/autoload.php';
    

    使用 define() 已经不常见了。

    根据环境不可知的方法,使用 .env 或者类似的。

        8
  •  0
  •   mario Nizam Mohamed    14 年前

    这是一个非常好的方法,只要你的网站不超过20页的门槛。不过,我建议在函数样式中使用include(),而不是作为构造,并将这些模板放在单独的子文件夹中。如果其中没有PHP代码,也可以使用.htm文件扩展名(指定部分html的htm)。

     include("template/main/header.htm");   // would still parse PHP code!
    

    这种方法的缺点是,最终会通过全局变量向其中注入HTML。 $HEAD='<link...>'; include("../header.htm") . 这本身并不坏,但可以迅速积累原油。

        9
  •  0
  •   Can O' Spam    8 年前

    我知道这已经很晚了,只是想在这个问题上加上我的“便士价值”。

    我的建议是为此创建方法,例如我的根是:var/www/htdocs/函数文件位于includes/functions/template中-零件.php.
    我的职能如下:

    <?php
    
    define("DOC_ROOT", "/var/www/htdocs/"); # Add a trailing slash to make it a little easier
    
    function GetHeader()
    {
        return include DOC_ROOT . 'includes/parts/header.htm'; # Header found at include/parts/header.htm
    }
    
    function GetFooter()
    {
        return include DOC_ROOT . 'includes/parts/footer.htm'; # Footer found at include/parts/footer.htm
    }
    
    ?>
    

    并用作:

    <?php
    
    # From the main page (/var/www/htdocs/index.php)
    require_once 'includes/functions/template-parts.php';
    GetHeader();
    ?>
    <!-- Page content -->
    <?php
    
    GetFooter();
    
    ?>
    
        10
  •  -1
  •   Mark Snidovich    14 年前

    我喜欢用函数来打印页眉和页脚,而不是包含。这样可以更好地微调变量范围。