PHP包括最佳实践问题 [英] PHP include best practices question

查看:103
本文介绍了PHP包括最佳实践问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习PHP的语法并练习它。我来自.NET背景,所以当页眉和页脚出现时,masterpages总是让我很容易。

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.

到目前为止,我有一个mainHeader.php和mainFooter.php,它有我的头部菜单和我的页脚html。我创建了一个mainBody.php,在顶部我放了

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

和我放的页脚

<?php include "mainFooter.php" ?>

这很有效,让我微笑,因为我的网页很好地融合在一起。 mainHeader有我的< html> < body> ,我的mainFooter有我的结束标记。

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.

这是一个好习惯吗?

推荐答案

我包含了我的观点我的控制员。我还定义了文件位置以便于维护。

I include my views from my controllers. I also define file locations to make maintenance easier.

config.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 <我需要的所有信息/ code>。

Now i have all the info i need just by including config.php.

controller.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 );

这篇关于PHP包括最佳实践问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆