简体   繁体   English

PHP项目的文件结构

[英]File Structure for a PHP Project

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; functions.php我有一个通用函数来生成HTML的<head>部分; also, a function to return an absolute path from the document root. 还有一个从文档根目录返回绝对路径的函数。 Note my attempt to calculate /holiday/includes/ . 注意我尝试计算/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: functions.php是通过以下方式包含的:

// 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() ? 我的get_path()任何内置的PHP函数吗? 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 : 相关讨论

@Siku-Siku.Com, auto-loading classes with __autoload() won't really help your main problem. @ Siku-Siku.Com,使用__autoload()自动加载类并不能真正解决您的主要问题。 Besides, you'll only be able to get the most out of __autoload() if you move to a mainly object-oriented design, which will bring its own challenges. 此外,如果您转向主要面向对象的设计,您将只能充分利用__autoload(),这将带来自己的挑战。

Currently, the most sensible thing to do would be as @hafichuk suggests. 目前,最明智的做法是@hafichuk建议。 Make one main includes file, say my_funcs.inc.php, and include it at the top of every other page you have. 使一个主要包含文件(例如my_funcs.inc.php)包含在其他所有页面的顶部。 The advantage is that by giving special .inc extensions to your include files, you can distinguish them more easily. 优点是,通过为包含文件提供特殊的.inc扩展名,可以更轻松地区分它们。 Plus, you can use that to block these files in Apache for just an added bit of security. 另外,您可以使用它来阻止Apache中的这些文件,从而增加安全性。

If I could also mention: 如果我还能提到:
1) I think short tags are risky. 1)我认为短标签有风险。 They encourage bad coding practices and leave the door wide open for porting nightmares. 它们会鼓励不良的编码习惯,并为移植噩梦敞开大门。 And they encourage bad coding practices. 并且他们鼓励不良的编码习惯。
2) Since require is a statement, not a function, it should be used like: 2)由于require是语句,而不是函数,因此应按以下方式使用:

require 'my_file.inc.php';

With the type of layout that you currently have, your best bet is to have a single includes.php file which holds all of your require_once calls, then use require_once('../includes.php') (or equivalent location) at the top of each of your entry scripts. 对于当前使用的布局类型,最好的选择是使用一个includes.php所有您的require_once调用的require_once文件,然后在页面上使用require_once('../includes.php') (或等效位置)每个输入脚本的顶部。 It's a pain to setup and maintain, but at least it's all in one place. 设置和维护很麻烦,但至少要放在一个地方。

If you plan on moving towards using objects instead of functions, then I'd take a look at using __autoload() . 如果您打算使用对象而不是函数,那么我来看看使用__autoload()

After experimenting more with this and gathering other inputs, I find using constant will do the trick: 在对此进行更多实验并收集了其他输入之后,我发现使用Constant可以解决问题:

# 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 eg /vacation/ , you'd simply need to change one constant ie PREFIX . 因此,如果您需要将/holiday/移至其他子目录(例如/vacation/ ,则只需更改一个常量,即PREFIX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM