What is the best way to cleanly structure PHP sites without using a framework?
I'm working on a medium sized PHP site. I'm writing "provider" classes for my objects. Basically, if I have a "user" object, I have a "user-provider" class that can request / save users on the database, accepting and returning user objects. I just include the providers and the dependent objects in the files I need them.
I'm wondering if this is a clean way to structure a PHP site without a framework, so I'm interested in hearing from others what the best way to str开发者_开发技巧ucture a PHP site is without using a framework.
First of all, there's nothing wrong with using a framework, as long as it doesn't introduce unnecessary complexity and/or performance penalties for small applications. In PHP, the Symfony and Laravel frameworks are quite good in that regard.
Having said that, if you really don't want a "foreign" framework, you'll end up writing your own one anyway. So it's better to plan for that. Just take you favorite framework and structure your project exactly the way you would do when using that framework. Then, write the glue code (i.e. your own mini-framework) yourself. In case you notice that your project grows bigger than expected, you won't have much trouble switching to a "real" framework.
However, if you really don't want to use anything that resembles a framework, because your application is really damn small, just put everything into one file. Within that file, you should of course keep a clean separation between presentation, CSS, application logic etc.! But in a minimal application there's no need to get into the hassle of multiple files.
The following recommendation emerged as a consensus among many experienced PHP developers:
myproject/
application/
configs/
- ...
controllers/
- ...
views/
- ...
models/
- ...
- ...
library/
PEAR/
Zend/
- ...
public/
← document rootindex.php
library/
editarea/
jquery/
OpenLayers/
- ...
media/
- ...
styles/
default.css
- ...
- ...
Although this structure was initially coined in the Zend framework, it is advisable for any project, no matter which framework it uses or whether it uses a framework at all.
Starting with that structure from the beginning, you'll be able to easily switch to a real framework when your application grows.
Some important notes about this structure:
- The document root is a direct sub folder called
public/
which is an accepted standard not only in PHP but also in Python, Ruby, etc. - The folder names are technology-neutral. (
library/
instead ofjs/
,styles/
instead ofcss/
) - This structure still needs some discussion. For instance, it would be better if the directories were named all singular or all plural, not a mix like
library
andstyles
. Also, one might argue that MVC is not always advisable, or shouldn't be named as such, so a more generic folder name liketemplates/
rather thanviews/
would have been a better choice.
This is a good starting point: http://www.phpguru.org/static/ApplicationStructure
$_PATHS["base"] = dirname(dirname(__FILE__)) . "/";
$_PATHS["includes"] = $_PATHS["base"] . "includes/";
$_PATHS["templates"] = $_PATHS["base"] . "templates/";
$_PATHS["pear"] = $_PATHS["base"] . "pear/";
$_PATHS["logs"] = $_PATHS["base"] . "logs/";
But basically what you end up with... is a framework :)
Model view controller is also nice to have. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
精彩评论