1880 S Dairy Ashford Rd, Suite 650, Houston, TX 77077

Building a custom CMS with PHP from scratch

It could be difficult for beginner PHP developers to create anything by going back to the drawing board and implementing unique strategies to it. However, the right development strategies can save most of your time while working on any project concerning with a particular tech stack. This is a short guide to building a custom CMS website with PHP from scratch with a little bit of code snippets along the way. So let’s begin. 

Step 1: Create a database

First step is creating a database. MySQL goes well with PHP but if you are comfortable with some other database, go for it but make sure it can work with PHP. Following are the steps in creating the database. 

  1. Run mysql, in terminal enter following:
    mysql -u username -p. 
  2. Enter your MySQL password and a username with database creation permission. Use the root user on a development server to save time.
  3. Create the database through command:
    create database cms;
  4. Quit the mysql client program or type”exit” at mysql> prompt and Enter.

You’ve created a new, empty database.

Step 2: Create required tables

Next step is to straighten out the database and populate it with tables. Use the following commands:

The above SQL code defines the articles table schema. If you cannot understand the SQL queries or do not know PHP enough then do know that this is not a tutorial. This is just a guide that will help you clear the idea and give some help.

Following is the table structure for the content management system.

  • Creating an ID field having smallint unsigned data type. It can hold up to 65,535 articles.
  • Specify NOT NULL to prevent empty fields.
  • Add auto_increment to assign a unique value to each article.
  • Use the unique value to refer to articles in the CMS.

“publicationDate” field has a “date” data type and stores the date on which each article was published. “title” field has varchar data type and holds each article’s title up to 255 characters. Summary and content fields store short article summary and have text data type.

Having a primary key for the table is very important, for storing, retrieving, and updating the data. “ID” field is our primary key so it uniquely identifies each record.

Add this table schema to MySQL by opening terminal window and folder for tables.sql through 

mysql -u username -p cms < tables.sql command

Step 3: Configuration file for CMS website settings

A configuration file to keep all settings for the content management system is important if you do not want to do it manually everywhere. First create a folder named CMS in the root directory. For XAMPP, the root directory is htdocs. Create a config.php file inside the created folder. Then add the following code:

Step 4: Build the Article class

For quick CMS this is the only class you need. For now start with this and later you can add more tables to the database and build more classes as required. This class will be dealing with all the data from our. 

Create a folder named classes inside the CMS folder. In “classes” create a file called “Article.php”.

In the file add following things to get the code started.

Class definition and properties

Define a class and write down its properties. Follow an object-oriented design pattern.

Constructor 

Create a constructor method which will be tied to the class and its objects. This is a handy way to populate classes .

storeFormValues()

The storeFormValues() method is similar to the constructor method because it stores array data but this method is able to handle the data which New Article and Edit Article will send. 

getById()

This method will access the database, accept an article ID then get required data like article record and store it in a new object.

getList()

The getList() method is similar to the getById() method but the difference is this gets all the articles at once. You might be wondering why 2 methods just to get articles. This method will be used to get all lists of articles.

insert()

This method, as the name suggests, will be used to insert the data into the table. 

  • Make sure that the object to be inserted does not already exist.
  • If it does then do not insert but update.
  • Run SQL query to insert record.

update()

Insert, update, and delete methods are necessary for every project whether it is a custom content management system or not. First get the object’s ID to update it. 

delete()

This is a self-explanatory method. Just like the update method, you should get the object’s ID to access its data in the database and successfully delete it. For safety reasons, limit the query to 1 so that multiple records are not deleted accidently. 

Step 5: Create the frontend & connect with backend

Now all you have left to do is create a stunning frontend for your custom PHP CMS website. You can take inspiration from others content management system websites or even wordpress themes. Once your frontend is ready, get it connected with the database through PHP. For creating frontend you would have to use a combination of languages like HTML, CSS, JavaScript, some XML and Ajax. This is enough for creating a simple and quick CMS website. However, if you want to get things to the next level then using a JS framework is the next step. If you do not know how to get started then to hire PHP developers is the best option. Now, with remote developers available at cheaper cost things are easier than ever.