PHP web project directory structure

Hello

I am a Java developer who is trying to learn how to write object oriented PHP. I am in the process of trying to write my first PHP web application as a learning opportunity (not for a client). So I am trying to establish good habits.

I have downloaded XAMPP, Eclipse and setup the PDT plugin for Eclipse. Now, my first goal is to establish a good working directory structure.

My first question is, are their directories that need to be created for the PHP framework? For example, in Java I have to have a WEB-INF directory which contains a web.xml file and a classes and lib directory. Does PHP have any similar technical requirements?

My second question… below is an example of how I would setup a web application directory structure for Java / JSP. How would I setup a good directory structure for PHP?


css (style sheets)
	|
	|-- main.css
 |
html (all static HTML content)
	|
	|-- contactUs.htm
	|-- index.jsp
 |
images (all images for the site)
	|
	|-- mainImage.jpg
 |
inc (include files)
	|
	|-- disclaimer.inc
	|-- footer.inc
	|-- topMenu.inc
 |
js (all JavaScript)
	|
	+-- page (JavaScript specific for a page)
		|
	|-- utility.js
 |
jsp (All dynamic content - JSP pages)
	|
	|-- test.jsp
	|-- upload.jsp
 |
WEB-INF
 |
 +-- classes (Java classes)
		|
		+-- com.xxx.dao
			|
			|-- IDao.java - interface for a Data Access class
		|
		+-- com.xxx.dao.impl
			|
			|-- MySqlDao.java - implementation of IDao
		|
		+-- com.xxx.util
			|		
			|-- DataAccess.java - tasks related to connecting to DB
 |
 +-- lib (Contains all .JAR files)
 |
 |-- web.xml

Any feedback you can provide is appreciated.

Thanks in advance for your time.

If you are mentioning frameworks, most of them are recommending certain directory structure.

For example, if you choose Zend framework, you can find the recommended directory structure right in their framework documentation or even tools that create a project structure for you:
http://framework.zend.com/docs/quickstart/create-your-project

Thank you for the post but I really am not looking for a specific PHP framework. Instead, I am looking for the best practice for putting together a directory structure for PHP code. I found this site:

It outlines something like this (I took this from the link above):

/

  • Webapp root directory. Contains the pages that are actually called by the browser.

/lib/

  • Contains base.inc.php and config.inc.php

/lib/common/

  • Contains libraries and tools reusable for other projects, like your database abstraction classes.

/lib/model/

  • Contains the Value Object classes

/lib/dao/

  • Contains the DAO classes and the DAO factory

/lib/logic/

  • Contains the business logic classes

/parts/

  • Contains partial HTML that is included by pages

/control/

  • Contains the page logic.

Is this what most people follow? With Java / JSP, there are best practices for building a directory structure that promote separating areas of concerns and create project organization which can prove to be very beneficial as the size of your code base grows. I was curious if PHP had a best practice.

In the example I posted, do the PHP files such as index.php, constactUs.php go in the Webapp root?

I would personally put web-accessible files into their own directory.

What I do, at bare minimum, is:

  • configs/ – configuration files
    [list]
  • site.yaml/ini
    [/list]
  • build/ – build scripts, to minimize the filesizes of CSS/JS/etc. files*
  • lib/ – all the libraries
  • site/
    [list]
  • lib/ – sometimes I put site-specific libraries here
  • public/ – actual web-accessible files
    [list]
  • css/
  • images/
  • js/
  • whatever.php**
    [/list]
  • views/ – templates
  • controllers/ – if the site has a single entry point, then controllers are here
    [/list]

*I try to put websites into a SCM, so when I push to the production server, it automatically runs the build scripts and then copies the files to the live site.
**Though I rewrite URLs so that .php isn’t seen in the URLs if I don’t use a single entry point.

There’s really no “right way” to do it, seeing that PHP grew from a hack language. Just look at several examples and choose the one you like the most.

Thank you for the post. Here is my first attempt. Any feedback is appreciated.


+-- config
|
+-- lib
      |
      +-- control
            |
            |-- insertData.php
      |
      +-- dao
            |
            |-- IDataDao.php
            |-- MySqlDataDaoImpl.php
      |
      +-- logic
            |
            |-- DataBean.php
      |
      +-- model
            |
            |-- DataValueObject
      |
      +-- view
            |
            |-- is this where you would put other PHP presentation files?
|
+-- web
      |
      +-- css
      +-- html
      +-- images
      +-- js
|
index.php

Correct me if I am wrong but index.php has to be at webapp root level correct? If so, do I put other presentation php pages in the view directory in lib?

Hmm, well, PHP has no understanding of an “application.” You just toss PHP files into a directory, set a web server to serve from that directory (and have it parse PHP files), and each script file is run independently from all other script files.

So where index.php resides entirely depends on how the web server is set up. If it were me, I’d point it to web/ (public/ in my example), so that all the other folders (config/, etc.) can’t be reached from the web. It doesn’t even have to be named index.php – that filename isn’t special to PHP. It depends on how the web server is configured in regards to the index files it needs to look for.

If you want a proper MVC and application-centric organization, you will have to set it up yourself (or download a framework someone else has written). The entry points of your sites are the files in the directory that you set your web server to serve. Some web servers let you configure them to send all requests to one file, and you can do that, but how all the other files get involved (configuration files, models, etc.) is entirely dependent on the code you write.

The only real feature of PHP that helps tie a bunch of script files into some form of application is the PHP include path, which makes it easy to include files from set of directories without having to specify a full path.

Thank you very much for the feedback. Each day that I am coding PHP I am learning more. I really like the MVC (Model-View-Controller) approach but that is because of my Java background. I have also found out about the include statement when I am writing using the MVC approach. But most of the time, I use include_once… that is correct isn’t it? When I am writing code with an MVC approach, I want to include my class files using include_once instead of include, correct? I assume the web server has to download 1 file less amount of times.

It’s fine to do MVC. It’s just that PHP won’t set it up for you.

include and include_once are self contained on the server and in the PHP process interpreting your script, so nothing gets transferred. It’s just basically as dumb as C/C++'s #include preprocessor statement (though not so much now, with PHP 5.3), where you’re basically just pasting another file into where you put the include statement. Now, of course, using include_once means that PHP won’t have to re-include a file, but that’s negligible performance loss.

If you are including files with classes or functions, then you should use include_once because if you include the same file again (perhaps because you have two script files that depend on the same files and one includes another), you won’t redefine a class or function. PHP will throw a non-catcheable non-exception fatal error if you attempt to redefine a class or function.

Although be aware that there is also a require(_once) too. include(_once) won’t throw an exception or stop your script’s execution if it fails to include. require(_once) will cause a non-catcheable non-exception fatal error.

Grouping by views/controllers is rather short sighted.

I organise mine by “module” which contains all the templates, controllers, models etc which are used by that part of the site. This way I can lift a module directly from one project to another by just copying the folder. A module is something like a poll, forum, CMS, login system, admin area.