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:
- 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. - 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 yourcontacts_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:
index.php
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 common framework operation routine as follows:
index.php
).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.