Learning a CMS is the start of professional web development. If you have already used some CMSs such as Word Press, Drupal, Joomal etc then you will already know what a CMS is, how they look and how they work. However, even if you have not used any of those CMSs before you do not need to worry as we will not be using elements of those systems.
What is a CMS?
CMS stands for Content Management System. It is a way to manage content (your data) on a server. Using a CMS makes your code modular, with web pages created dynamically. A CMS makes code much more sensible and readable. For example, in a dynamic web page, we have a header (the top section of the page), sidebars, a footer (bottom section), content from a database (most things are stored in the database except files) and some functions & classes for handling the web page (like manipulating database, form validations, security handler, etc.). All of these sections are placed in different files and combined in order to generate a web page. For more read CMS vs. MVC frameworks.
Requirements:
- For this application you must have a basic knowledge of HTML, CSS and PHP. I will also use very basic Jquery. If you don't know anything about jquery then you will still be able to understand this series.
- An install of LAMP, WAMP or XAMPP according to your operating system.
- PHP 5.4+
- Bootstrap (we will download it later; you need not to know anything about it. It is a pre-written collection of css and js files which provide a liquid layout and essential theme. It will reduce our time so that we can put our focus on PHP).
If you are very new with php then use my step by step php tutorials Learn PHP
Why use this series?
This series is only for learning purpose so I am keeping it quite simple. It is far away from professional level but not so far that I cannot cover common issues. It is for people who can build a static or dynamic website but at a very low level.
In this series I will make a To Do Application with the following functions:
- Log in, Log out and Register users
- Create a to do entry under different labels
- Save a due date and calculate remaining time
- Show the progress of the work
- Edit and delete any entry
- Admin Section for handling themes and users
The CMS will have following qualities:
- Portability: Just like word press, it can have custom themes and widgets.
- Perfect distribution: It will be very modular with different files and folders.
- Secure: Here we are building a very small application but we will consider all types of security issues for best developing practice.
- Admin interface: In our case, admin interface is just managing themes and users, but you can extend it by adding more functions.
- Pretty links (Routing): Just like an MVC framework, this CMS has a routing system.
The code is open source and available in my github repository. If you don't want to copy the code from here then you can download a zip file from there. Live demo is At to Do Live Demo. Admin section is at ADMIN SECTION.
How does this CMS work?
As mentioned before, the .hatches file will redirect all requests to index.php file in the root folder. First of all the index.php file will define the security variable which will be checked by each file so that no-one can access any other file directly. In other words, all other files (except static files) can be opened only by the index.php file.
Then configurations from the config.php file are fetched and stored in constants so that other files can use them. After that we define a session variable with the name CSRF. This variable will be verified when we submit any form. It ensures that the form submission request is coming from a web page which is served by our website.
After this we call the Template Function class from the TemplateFunction.php file. Template Function registers the theme and includes index.php file from the theme folder (theme folders are in the templates folder). So how are web pages generated in the index.php file inside themes? We will learn this in the series.
Prepare the .hatches file
The .hatches file is used by the server. It provides a way to make configuration changes on a per-directory basis. The requests which are passed to sodalist directory will be filtered by this file.
The first line is telling us that rewrite mode is turned on. The second and third lines are collecting all requests for files or directories. The fourth line removes those requests which are static requests. The fifth line is sending all other requests to the index.php file. For more detail on the .hatches file read official document of Apache.
This code is well commented. Why is ‘connection’, an array of connections defined instead of using one connection? It is a port for multiple database connections. I am using it because I am using the same code in both local and production servers. I only have to change the value of ‘database’ to correct the connection. We will register our application as ‘apps’. Here we are defining three applications:
- Auth: It will handle the authentication (log in, log out, registration, reset password).
- To-do: This is our application 🙂
- Admin: This is our admin section.
We will create these apps in future tutorials. Note that this file has no closing tag for php. This is to remove extra white space as the PHP tag does not need to be closed.
Source: Findall Together