Php file structure best practices

When starting out with PHP, it can be daunting figuring out how best to organize a project. If you've ever been confused with where to put your images, external libraries, or keeping your logic separate from your layout, then check out these tips; they'll get you heading in the right direction.

Tutorial Details

  • Program: PHP/Projects
  • Version: 1
  • Difficulty: Easy
  • Estimated Completion Time: 20 minutes

Directory Structure

I'd say the number one thing in getting your project up and running quickly is having a solid directory structure you can reuse for multiple projects. If you are using a framework, usually it will provide a structure to use, but in this scenario we're working on a simple site or app.

Breakdown

  • You are probably very familiar with the public_html structure. This is the Document Root in which all your public files are accessed [/public_html/page.php is accessed at example.com/page.php].

    • img — All your image files. I decided to split content images from layout images.
    • css — All your css files.
    • js — All your javascript files.
  • The resources directory should hold all 3rd party libraries, custom libraries, configs and any other code that acts as a resource in your project.

    • config.php — Main configuration file. Should store site wide settings.
    • library — Central location for all custom and third party libraries.
    • templates — Reusable components that make up your layout.

The Config File

As designers and developers our main goal is to do as little work as possible. One way to reach this goal is with config files. To get a better idea of what the configuration file should have check out this example.

Chủ Đề