views:

96

answers:

4

PHP is a largely flexible language, I could do this anyhow and anywhere. However, what would you consider to be best practices for structuring large projects?

I have built a bespoke CMS for a client, but as it was my first venture into a large project its a bit of a mess. There's files everywhere, with a very bad folder structure.

A friend suggested that the only files that should sit in the root directory of the website are the "page" files - i.e. the files that actually have HTML in them and that you should have separate folders for each of the different elements, which can then be easily pulled into different pages with includes. Would everybody recommend this route or is it governed entirely by personal preference?

+2  A: 

I would recommend having only one file in root folder, being index.php, and make this file include everything else that's needed based on requests received. If you do OOP, it's usually convienient to put your classes into folder structure that mimics your namespacing (whether simulated in PHP <5.3 or actual in 5.3+). This allows for easy autoloading of classes.

Mchl
I belive this is a beginning to making alot of bloat in your code, and unfortunately, this way is quite common.
BarsMonster
@BarsMonster Since the question asks for large PHP projects and not some smallish site, it's a reasonable suggestion to use FrontController. The folder structure is also a good call. It's PEAR convention afaik. Compensating downvote with upvote.
Gordon
@BarsMonster: Arguably, if you put one huge switch() statement into index.php it's possobily the worst way to do this. However there are ways to do it in much leaner way (I'll refer you to frameworks which you mentioned in your answer)
Mchl
Ok, you win :-)
BarsMonster
+3  A: 

If starting a new project, consider using a Framework, or taking a look at how they do it.

Frameworks usually either dictate, or suggest, a directory structure.

Even if you don't end up using one: The way they structure their projects usually comes from tons and tons of experience.

I personally like the Zend Framework way of structuring, and its Autoloader, best. But there are several options and philosophies out there.

Framework questions:

Generally related:

Pekka
+1  A: 

Personally I use the way you described in last paragraph, and find it useful, clean & fast to execure. Be sure to move all common stuff in common libs/files, in no way should you copy/paste.

Another (common) way of structuring is when you use some frameworks, which usually enforce some specific project structure.

BarsMonster
+2  A: 

I'd say it doesnt matter too much how you layout your application folderwise. Having files is a necessity and it has some impact on the application. Address that impact. It's not how you layout your folders, it's why. Find some ideas below.

For security, you should put only those scripts into the webroot that are actually supposed to be directly servable to the world.

Also, if you have files created during runtime, make sure those dont get overwritten when you deploy/update your application to your server. Whether you address that in your deployment routine or by the folder layout is up to you.

For performance, take into account that including files/code that are/is not needed for a particular request will unneccessarily slow down your app. So does having many include_paths to search in. Make sure PHP can find the files it needs fast.

For maintainability, use common sense. There is no rule that you have to have one file per class. There is also no rule that you have to follow PEAR file layout. PEAR is a widespread convention and a reasonable one, but throw your files into a single folder if it makes sense for this particular application.

IMO, an application is not the files, it's the code within. It's much more important to have your code well organized than your files. Having a messy file layout is much less an issue than having a messy architecture.

Gordon