When implementing a blog for TaosNet, it needed to be designed in a way that it is easy for other people to be able to read, modify, and easily understand.
The following design decisions were made up front:
- Use MVC style code separation.
- A bunch of small files for the Controller, rather than one all inclusive file.
- Use apacheβs mod_rewrite to handle funneling all URLs to a single entry point script.
am going to assume that you understand what MVC is. If not, please read
the link above before continuing. The basic separation is: php files
are the Controllers, the database is the Model, and the TinyButStrong templates are the views.
An easy way for me to create an easy to understandable control flow was to have the main entry point (the target of the mod_rewrite rules) handle all the routing decisions. It would analyze the basic URL and form input and decide what action needed to be taken. Each action is its own php file that then processes the form input and updates/retrieves data from the database and sends the data to the view template to be displayed.
Building the database for the posts and comments is pretty easy. The minimal fields required for a post are an author, a date, a title, and content. The table structure for this is:
CREATE TABLE posts(id INTEGER PRIMARY KEY NOT NULL, author TEXT NOT NULL, date INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, title TEXT NOT NULL, post TEXT NOT NULL)
Comments are slightly more complicated. To implement comment threads, they need to be arranged in some sort of recursive structure. Tree structures are well suited to this. In addition, comments have an author, a date, and a comment body. The table structure is:
CREATE TABLE comments(id INTEGER PRIMARY KEY NOT NULL, post_id INTEGER NOT NULL, parent_id INTEGER NOT NULL default 0 , author TEXT NOT NULL, date INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, comment TEXT NOT NULL)
Php was originally designed as a template language, so why use another? Php has evolved into more than just a template language, and as such can be difficult for a someone who is not primarily a php coder (think html/css) to follow. TinyButStrong is a simple but easy to understand template language. It is much faster to learn than php and is easier to avoid shooting yourself in the foot when you are just interested in getting an HTML view that you can send to the browser.
Coding your own blog is not terrible difficult, and comes down mostly to planning. I hope you enjoyed this segment, and feel free to leave comments and suggestions.