PHP项目的文件结构 [英] File Structure for a PHP Project

查看:214
本文介绍了PHP项目的文件结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件结构:

  /holiday/admin/list.php 
/ holiday / includes / functions。 php#常用函数
/holiday/index.php

#/是文档根目录
#/ holiday /是一个独立的子目录
#还有其他自包含的子目录,例如/ promotion /,/ international /

functions.php 我有一个通用的函数来生成HTML的< head> 部分;另外,还有一个从文档根目录返回绝对路径的函数。 注意我试图计算 / holiday / includes /

 < ;?函数get_path(){
//技术上,这返回dirname(__ FILE__) - $ _SERVER ['DOCUMENT_ROOT']
返回str_replace($ _ SERVER ['DOCUMENT_ROOT'],,dirname(__ FILE__)) ;
}?>

<?函数open_page($ head =,$ body_id =){?>
<!DOCTYPE HTML>
< html>
< head>
< link type =text / css =stylesheethref =< ;? echo get_path()。/../css/savvyextras.css;?> />
< script type =text / javascriptsrc =< ;? echo get_path()。/../scripts/modernizr.js;?>>< / script>
...
<? }?>

functions.php

  //从list.php 
require_once('../ includes / functions.php');
open_page(...);

//从index.php
require_once('./ includes / functions.php');
open_page(...);

我觉得在这里必须有更直接的方法来完成同样的事情。对于 get_path()的任何内置PHP函数?也许我应该以不同的方式处理我的问题?


有些人建议使用框架(这是一件好事)。但是,为了帮助我(和其他人)理解整个包含文件的事情,还有其他的非框架解释?

p>


解决方案

经过更多的尝试,并收集其他投入,我发现使用常量将做的伎俩:

$ p $ #functions.php

define('PREFIX','/ holiday');

<?函数open_page($ head =,$ body_id =){?>
<!DOCTYPE HTML>
< html>
< head>
< link type =text / css =stylesheethref =<?php echo PREFIX;?> /css/savvyextras.css; ?>/>
< script type =text / javascriptsrc =<?php echo PREFIX; ?> /scripts/modernizr.js;?>>< / script>
...
<? }?>

所以,如果您需要移动 / holiday / 到不同的子目录,例如 / vacation / ,你只需要改变一个常量即 PREFIX


My file structure:

/holiday/admin/list.php
/holiday/includes/functions.php   # common functions
/holiday/index.php

# / is the document root
# /holiday/ is a "self-contained" sub-directory
# There are other "self-contained" sub-directories e.g. /promotion/, /international/

In functions.php I have a common function to generate the <head> part of an HTML; also, a function to return an absolute path from the document root. Note my attempt to calculate /holiday/includes/.

<? function get_path() {
  // Technically, this returns dirname(__FILE__) - $_SERVER['DOCUMENT_ROOT']
  return str_replace($_SERVER['DOCUMENT_ROOT'], "", dirname(__FILE__)); 
} ?>

<? function open_page($head = "", $body_id = "") { ?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<? echo get_path() . "/../css/savvyextras.css"; ?>" />
<script type="text/javascript" src="<? echo get_path() . "/../scripts/modernizr.js"; ?>"></script>
...
<? } ?>

functions.php is included this way:

// From list.php
require_once('../includes/functions.php');
open_page(...);

// From index.php
require_once('./includes/functions.php');
open_page(...);

I feel like there must be a more straightforward approach to accomplish the same thing here. Any built-in PHP function for my get_path()? Maybe I should approach my problem differently?

Note: Some folks suggested using a framework (which is a good thing). But, to help me (and others) understand this whole include-file thing, other non-framework explanations?

Related Discussions:

解决方案

After experimenting more with this and gathering other inputs, I find using constant will do the trick:

# functions.php

define('PREFIX', '/holiday');

<? function open_page($head = "", $body_id = "") { ?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="<?php echo PREFIX; ?>/css/savvyextras.css"; ?>" />
<script type="text/javascript" src="<?php echo PREFIX; ?>/scripts/modernizr.js"; ?>"></script>
...
<? } ?>

So, if you need to move /holiday/ to a different sub-directory e.g. /vacation/, you'd simply need to change one constant i.e. PREFIX.

这篇关于PHP项目的文件结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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