Creating a Content Management System (CMS) can be a daunting task, especially for those who are not familiar web development. However, with the right tools and knowledge, it is possible to build a "simple" flat-file CMS using PHP.
A flat-file CMS is a lightweight and easy-to-use system that does not require a database to store content. Instead, it uses text files to store data, making it ideal for small websites or blogs. In this article, we will explore how to create a flat-file CMS using PHP.
VERY IMPORTANT: this simple tutorial is only for educational use. It is not intended to be used on a live internet accessible server. Please only use it as a leaning tool and only use it on your local host server.
First, let's start by creating the file structure for our CMS. Create a new folder on your server and name it "cms." Inside this folder, create the following sub-folders: "content
," "templates
," and "assets
." The "content" folder will store all the text files containing our content, the "templates" folder will store our HTML templates, and the "assets" folder will store CSS, JavaScript, and other files.
Next, we need to create a few template files. Create an "index.php
" file inside the "templates
" folder, and another file called "header.php
" and "footer.php
." In the "header.php
" file, add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flat-file CMS</title>
<link rel="stylesheet" href="./assets/styles.css">
</head>
<body>
In the "footer.php" file, add the following code:
<script src="./assets/script.js"></script>
</body>
</html>
Now, let's create a function to read the content from our text files. Create a new file called "functions.php
" inside the "cms
" folder and add the following code:
function get_content($slug) {
$content = file_get_contents("content/{$slug}.txt");
return $content;
}
This function will read the content from a text file with the specified slug and return it as a string.
Next, create an "index.php
" file inside the "cms
" folder and add the following code:
<?php include 'templates/header.php'; ?>
<?php
if(isset($_GET['page'])) {
$page = $_GET['page'];
$content = get_content($page);
echo $content;
} else {
echo '<h1>Welcome to our flat-file CMS!</h1>';
}
?>
<?php include 'templates/footer.php'; ?>
This code includes the header and footer templates and checks if a page parameter is set in the URL. If a page is specified, it uses the get_content function
to retrieve the content from the text file with the corresponding slug.
Finally, create a text file inside the "content
" folder with the name of the page slug you want to display content for. For example, if you want to create a page with the slug "about," create a file called "about.txt" and add the content for that page.
That's it! You have successfully created a basic flat-file CMS using PHP. This simple system allows you to manage your content without the need for a database, making it easy to set up and use for small websites or blogs. You can further enhance this CMS by adding features like editing, deleting, and creating new pages, as well as adding styles and scripts to customize the look and feel of your website.
Stay tuned for a future tutorial on adding more features to this flat-file cms.