PHP Mini Sites CMS

In this fourth post we’ll have to actually do something useful with our code. Simply add the following at the end of the previous script.

Now we have everything set up. We only need to populate our theme with the relevant content. We’ll use pseudo tags in the theme, like {title}, {post} and so on.

We’ll place those meta tags in our template, or the Artisteer template. Then save this file as “theme.html” or whatever name you prefer to use.

Here’s an extra simple theme with no styles, just to show the concept:

This approach is slower than echoing the variables in the page itself, as it requires reading the theme and performing string replacements, but gives us a clean separation between the theme and the code, and makes this project easily scalable.

Makes sense? Hope so, because there’s not much more to be done. Save the script as index.php, then create or edit the .htaccess file. We need a rewrite rule to pass everything the user requests to index.php.
The .htaccess rewrite rule

Now, if there’s something I really don’t like to do it must be writing rewrite rules. I’m no Apache guru and have never studied them formally like I did for programming. But this one is really simple to use and understand.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

First, we turn on the rewrite engine and say everything is relative to the web root.

Then, there are two rewrite conditions, almost identical. They check if a file (-f) or a directory (-d) exists. If so, the existing file or directory is served.

Otherwise, the rewrite rule is invoked, and points to /index.php, our code. Note that no redirection is performed, so this will return an OK 200 status and the user’s browser will still display the originally requested url.

That’s almost all, folks.

Well, that’s all to be honest. The simple cms is ready.

However it’s still skinny, even after adding a nice artisteer template there will be no navigation menu, and this is the main issue for powering a real site.

We may also want to add RSS, a sitemap, a contact form, even some widgets. As long as there’s no user submitted content, such as comments, we can do everything without writing to our server. And we’ll see how in the next post.

Feel free to comment, ask questions and suggest how to expand this simple flat file php CMS. I’m not sure it’s worth to add an administrative panel, as this is really meant to be a set and forget script, but will wait for your suggestions.

Thanks for reading so far, have fun!

Posted in Complete Scripts
3 comments on “PHP Mini Sites CMS
  1. Jason says:

    loving this series – when can we expect the next post?

  2. PhpTips says:

    Ooops! Thanks for pointing this out, third post became too long and I split it in a few more, then forgot to add them. Part four is out now, and the mini cms is complete. But a fifth is planned to add some more bells & whistles.

  3. Jason says:

    Thanks for this – you’re the best.

Leave a Reply

Your email address will not be published. Required fields are marked *