PHP include best practices question

2020-01-27 00:39发布

I have been learning syntax for PHP and practicing it. I come from a .NET background so masterpages always made things pretty easy for me when it came to headers and footers.

So far I have a mainHeader.php and mainFooter.php which have my head menu and my footer html. I created a mainBody.php and at the top I put

<?php include "mainHeader.php" ?>

and for the footer I put

<?php include "mainFooter.php" ?>

This worked perfectly and made me smile because my pages all came together nicely. the mainHeader has my <html> and <body> and my mainFooter has my closing tags for those.

Is this good practice?

标签: php include
9条回答
▲ chillily
2楼-- · 2020-01-27 00:57

The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-01-27 01:01

I know this is very late, just wanted to add my "pennies worth" to this question.

My suggestion would be to create methods for this, e.g my root is: var/www/htdocs/ and the functions file is in includes/functions/template-parts.php.
My functions would look as such:

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

?>

And used as such:

<?php

# From the main page (/var/www/htdocs/index.php)
require_once 'includes/functions/template-parts.php';
GetHeader();
?>
<!-- Page content -->
<?php

GetFooter();

?>
查看更多
女痞
4楼-- · 2020-01-27 01:02

What you're doing is ok until you start using "Views" or "Templates" in which case you no longer arrange your content HTML inside the "controller" or "action" running.

Instead you will load a view and populate it with values which leaves all the HTML source ordering to the view and not your PHP file.

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

which then loads the layout file which looks something like this:

<!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>
查看更多
欢心
5楼-- · 2020-01-27 01:02

This is a perfectly fine method, as long as your site doesn't outgrow the 20 pages threshold. I'd however advise to use include() in function style, not as construct, and place these templates in a separate subfolder. If there is no PHP code in them, also use a .htm file extension (htm designating partial html).

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

The disadvantage with this approach is, that you somewhen end up injecting HTML through global variables into it. $HEAD='<link...>'; include("../header.htm"). Which is not bad per se, but can quickly amass cruft.

查看更多
女痞
6楼-- · 2020-01-27 01:03

You can also do it the other way round. Have a main page with header/footer and include only the body.

<!DOCTYPE html>
<html lang="en">
    <head>
        ...
    </head>
    <body>
        <?php include $page ?>
    </body>
</html>
查看更多
三岁会撩人
7楼-- · 2020-01-27 01:04

To summarize all the above.
That's good way to use includes, but do not forget to use a template page for the page contents.

Partly based on Galen's and 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 something like this:

<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 ?>
查看更多
登录 后发表回答