How to build a CMS: Defining the system

Dean OBrien
In this article, we will look at our content management system as a whole, to get an idea of how we will start to manage these content items.
This article is the part of a series of articles that intends to demonstrate ‘How to build a content management system’. If you are reading this in isolation, you may wish to consider reading the first article, which gives a brief overview of each step in the process and gives this article further context.
This article shows how a single PHP file can be used to act as an interface to the content within our database, controlling which views are displayed and actions are performed.

The first thing to note is that unlike other websites, there will only ever be one file presented to the web user, the index.php file. Every click, navigation or action that is performed on the website, will be directed back towards the same file. This file will then act as the controller for the whole site and depending upon what input is given, the file will determine what view is presented back to the user.

With this focus on one single file, we then gain greater control over exactly what happens every time an action is performed on the site. The following steps are performed every time the index.php file is called.

index-page-logic.pngProcess Input

Input is sent to the system in one of two different ways.

The first of which is by appending parameters to the URL i.e. index.php?id=1. In this example, the parameter being passed is [id] and it has the value of 1. We access this using the $_GET[]‘id’] command.

The second method uses a HTML form to POST data back to the system. With this method, the data is packaged in the HTTP header request, so it is not visible to the user. We access data sent by this method using $_POST[]‘id’] in our code. This is the preferred method, when performing any action that will make changes to our content.

Irrespective of which method is used, we process the input and store each parameter in a variable by the same name. So in the first example, we can access the PHP variable $id if it has been sent.

Process Actions

If the input that has been processed in the step above, requests a change to be made to some of our content items, then that action is then carried out at this stage.

Possible actions might be:
  • Add an item
  • Edit an item
  • Delete an item
  • Process login
  • Process Logout

The more you develop your system, the greater this list will become. For each of the actions, you will create a new block of code, which will only executed should the correct set of input be presented.

Render View

In this final step, the system determines what view should be shown to the user.

The system needs to determine, does the user want to:
  • View content items, or
  • Access the management area

View content items
To determine if a user wants to view a content item, the system looks out for one of two combinations of input:
  • Only [id] – in which case we assume navigation type and translate to [type] and [typeid]
    (i.e. index.php?id=1 -> translates nav [id=1] from table)
  • Both [type] and [typeid] – in which case we deliver specified content item
    (i.e. index.php?type=page&typeid=1 -> deliver page [id=1])
This is covered in more detail in the article How to build a CMS: Displaying the content

Access management area

We determine if the user wants to access this area by searching for the [display] parameter in the address bar. If this is found, we then display the requested view.

Examples of the type of views display might be:
  • List of items (with links to perform actions)
  • Forms (to provide input)
  • Search box
The code below shows the basic PHP framework that we will use to control what data is shown to the user:
                      		<h1><u>CMS Example #1</u></h1>
                      			<li>Find content item using 'nav' type <a href="index.php?id=1">click here</a>.</li>
                      			<li>Dislay type 'page' with id '1' <a href="index.php?type=page&typeid=1">click here</a>.</li>
                      			<li>Display login view <a href="index.php?display=login">click here</a>.</li>
                      /* Enable error reporting */
                      ini_set('display_errors', 1);
                      /* process input - create a variable for each input and assign the value*/
                      foreach($_REQUEST as $key => $value){ $$key = $value;}
                      /* process actions */
                      	echo "<p>PROCESSING ACTION...</p>";
                      		echo "<p>PROCESSING LOGIN...</p>";
                      		echo "<p>LOGIN SUCCESS... REDIRECTING TO DASHBOARD</p>";
                      /* find homepage */
                      	if($display=="login") {
                      		echo 	"<p>LOGIN VIEW<p>
                      			 <p>To login, please <a href='index.php?action=login'>click here</a>.";
                      	}elseif($display=="dashboard") {
                      		echo 	"<p>DASHBOARD VIEW</p>
                      				<li>To view all pages <a href='index.php?display=view&type=page'>click here</a>.</li>
                      				<li>To add a new page <a href='index.php?display=add&type=page'>click here</a>.</li>
                      	}elseif($display=="add") {
                      		echo "<p>ADD VIEW</p>";
                      	}elseif($display=="view") {
                      		echo "<p>DISPLAY ALL VIEW</p>";
                      	/* process nav to determine type & typeid */
                      			echo "<p>TRANSLATING NAV TYPE</p>";
                      			/*for example only assign type=page and typeid=1*/	
                      	/* display content item */
                      	echo "<p>DISPLAY CONTENT ITEM</p>";
                      	echo "TYPE=".$type;
                      	echo "ID=".$typeid;
                      	/* display home page */
                      	echo "<p>HOME PAGE</p>";

Open in new window

 In short, the code does the following:
  • Enables error handling
  • Processes input
  • Processes actions
  • Finds the home page
  • Output display if requested
  • Translates navigation if detected
  • Outputs content item if detected
The script above simply navigates between different views that will be available to the user (no further functionality has been added at this stage, and there are no connections to the database). The link below shows a working example of the code, as you navigate between the different sections, take note of the parameters in the address bar and how they relate to the sections within the code.

As you can imagine however, no user will be given access to these views without us having confirmed that they are authorised to see it.
To summarize, in this section we have shown how a single PHP file can control what is output is presented to the user, based on the input received, thus creating a single interface that we will later connect to other functionality, that will enable us to access and manipulate the content within our system.

​In the next article 'How to build a CMS: Displaying the content' we will show how the system identifies, retrieves and displays the requested content item, and also how we use the navigation layer to hide non-essential and possibly restricted content from the user. 
Dean OBrien

Comments (1)

Would you have any recommendations on how to make this add/edit blog entries instead of pages? I'm trying to just add blog functionality to a single page on a website without converting the entire site into a CMS.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.