简体   繁体   中英

How do I structure my PHP project?

I am about to embark upon yet another large PHP project. This time, I intend to have the project folder be tidy! So I have a few questions concerning keeping my project clean and DRY:

  • How do I differentiate between PHP source files and PHP files that should be accessed by the browser? In other words, how do I make it clear which PHP files gives output and which gives function or class definitions?

  • I am planning on separating my PHP functions into static classes, separated by subject, for instance database::create() or editor::write(). What are your thoughts on this?

  • I am planning on creating a PHP file "core.php" that will be included at the top of EVERY SINGLE PHP file in the project. This file will handle authentication and include base functions. Thoughts?

  • The project will be heavily Ajax based. This begs the question: Should my PHP functions be actual PHP functions, or should they be individual PHP files that takes GET or POST input? My plan is doing both . I create a folder for PHP source files and one for PHP "ajax" files (or something), and have the latter folder populated with files names by the functions they will perform. The PHP file will then simply contain a call to a function and write output for Ajax. Thoughts?

Any other comments or tips before I start this project would be great!


Edit

I may have failed to emphasize just how heavily Ajax based this web application will be. Codeigniter seems like a great tool for creating web pages or simple web applications such as a blog. My application will be a little different. Much like the uTorrent WebUI, my web application will be static on one page, having Ajax performing all the under-the-cover actions. It seems a little awkward to use Codeigniter for such a project.

Is there a different architecture out there created with this kind of application in mind?

Have you considered using a framework such as Codeigniter? Are you familiar with any sort of design patterns, such as MVC? Both of those would help dictate the location of files and separation of logic in your project.

EDIT: Codeigniter uses MVC, which is just a great pattern for web programming. Check out these two videos they have for tutorials. Only about 30 minutes combined, and definitely full of good knowledge, both as a look at the way their framework works, as well as the insight offered by their folder structure.

EDIT2: http://codeigniter.com/tutorials

If you're expecting this to be a large project I would highly recommend using one of the many established frameworks out there already like Symfony or Zend. You'd be:

  • cutting down drastically on the amount of code you'd have to write
  • avoid bugs in your components
  • learning best practices

But you if you insists on doing it yourself.

I am planning on creating a PHP file "core.php" that will be included at the top of EVERY SINGLE PHP file in the project. This file will handle authentication and include base functions. Thoughts?

I cannot stress enough 'do not do this'. There is a rule among experienced PHP developers that any project with a large core.php file that it's a warning sign of bad development and should be best avoided.

Secondly there the is no need to reinvent the wheel when it comes to database abstract, look at the pear MDB2 or Doctrine projects.

Large numbers of static classes are also a sign of poorly conceived development. Static classes are to be used sparingly as they are difficult to test, and often not needed.

Why not have one index.php that handles all of the execution and then have a .htaccess file that will direct urls to the correct location. This means that you can have all of you login and checking in one file.

You can then have a condition at the top of all of your other file's so they cannot be directly accessed like:

<?php if(FROM_INDEX!="true") die("<b>ACCESS_ERROR</b><br /><br />Please use the main page instead of accessing this file directly.");

I also recommend Codeigniter as this has a lot of features like this already and will greatly speed up your development time.

I'm not sure how complicated of a system you are looking for. But I recently found a small framework that works off the url. https://github.com/ivorychicken/sammy

If you don't mind taking the time to play with it, you could probably use it as a starting point and build off of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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