Php – good (neat) architecture in programming a simple website, e.g. a contact book

ArchitecturedesignPHP

When I build a simple website, e.g. a contact book where I can add, delete and update contacts, I create an index.php file where a user, if he's not logged in, is requested to enter a password and if he enters the right password, he's assigned a session and can do certain things with the contacts.

I have two files:

  1. The first (contacts.php) is for the HTML code to be shown. Above the HTML code I include the second file and create the class.
  2. The second (contacts_class.php) contains all methods for adding, deleting and updating.

I think that's ok, but when it comes to implement a big project, how should I do it? Do I have to create folders for every page and put files in them (like above, HTML and class), and how should I do it? What is a good and neat architecture for building large projects that every other programmer would understand perfectly?

Best Answer

You raised a very interesting and fundamental question. The question concerning the large scale project architecture and the folder structure organization (which is secondary to the architecture).

Today the most common approach to building the CMS framework architecture is the use of MVC pattern. There are some good articles about building your own MVC frameworks, one of them is Build an MVC Framework with PHP.

MVC stands for Model, View, Controller. You may call these approaches whatever you like - MVC, HMVC, MVP. The essence is to isolate the individual components of your system. The "Controller" retrieves the data from the "Model" and sends them to "View", which renders the final HTML. You have already implemented the "V" in your contacts.php and "MC" in your contacts_class.php. So you have isolated the view from the model and controller. Now you can easily change your "View" leaving other parts intact.

I am not suggesting you to blindly follow the MVC, MVP or whatever else "MV" pattern. It is the matter of appropriateness, efficacy and taste.

The common dynamic website application may include such components like:

  • The entry point, say index.php
  • The helper libraries / classes
  • The request router
  • The modules, components or controllers
  • The template engine or maybe single views

The real web application may include any other components like event handlers, event dispatchers and hooks, but these are in fact nuances. Well, let me present it the way I want to present it:

The operation routine diagram

The common framework operation routine as follows:

  1. The browser request is sent directly to the entry point executable / script (index.php).
  2. The entry point script loads the helper libraries, classes and performs some further initialization of our programming environment.
  3. The URL is passed to the request router instance. This step can be the part of step 2.
  4. The request router parses the URL and dispatches the operation to a particular component, module or controller.
  5. The component (or controller) processes the routed request and sends the data to the view to be rendered.

The corresponding project folder structure is shown in the diagram.

I would suggest you to investigate how the other frameworks are implemented. The recommended CMS / frameworks to begin with are CodeIgniter, OpenCart, Joomla 1.5 and Tango CMS.

Related Topic