Archives for March 2009
A High-Level Overview of the PHP + Javascript Start Page
If you haven’t checked out the PHP + Javascript Start Page head over to the extras page and give it a try. It’ll take you 5 minutes to get it running on your server.
Introduction
Sometimes I will get to looking around the web for a script to do this or that. Inevitably I will come up short-handed and decide to code my own solution. The PHP + Javascript Start Page was one such project.
The impetus
All I wanted was a page that I could add and remove links to sites that I visit frequently. I wanted to be able to open the mobile version of my webmail in-page and I wanted it to look good.
It ended up being an experiment in Object Oriented PHP and the Text DB API which provides a file-based database. I ended up with a pretty usable app, so I thought I’d share the wealth.
If you want to skip the technicalities, just go download it and have some fun. No programming required. If you want to see how it works, stick around.
Design Pattern
I used the Model-view-controller design pattern in the Start Page which started by defining three main classes:
- PageController.class.php
- BLL.class.php
- DAL.class.php
While it may be obvious that the PageController class acts as a controller intercepting events triggered by the user, it may not be obvious what the other two classes are for. The BLL or Business Logic Layer acts as the view. It translates data into a usable form which is made available through the controller.
The DAL or Data Access Layer is the equivalent of a model. It handles all the data interactions, basic CRUD (create, read, update, and delete).
Each of the classes uses a Link object, defined in the LinkObject class. A link object is an ID, title, and URL. That’s it.
There is also utility class which has a single function for retrieving parameters from the querystring.
Text Database
It annoys me a little when I have to setup a database in MySql to try out some piddly little application. For my own projects (which are typically piddly little applications) I have started to use the PHP Text DB Api. It is a great API used for managing a super-lightweight file based database.
Creating a database is as easy as creating a folder. And you can create a table by typing a couple of lines in a text file. Here is the only table used for the Start Page:
[code]
id#link#title
inc#str#str
##
[/code]
Looks a little cryptic but it’s really quite simple. The first line defines the names of each column separated by a pound sign. The next line defines the data type. inc is an auto incrementing data type. The final two pound signs mark the start of the data. Pretty straight forward.
Of course you could just fire the executeQuery() method in a PHP file with a simple CREATE TABLE statement. Yep. The Text DB API uses a subset of the T-SQL syntax. Very cool and simple.
So that’s the architecture and the database. The last part is the Javascript.
Lightwindowboxthing
I thought it would be cool to use one of those Javascript light boxes to display pages. Prototype JS was in a nearby folder so I just decided to go with it. I use both jQuery and Prototype on a regular basis and I don’t really care either way.
After a bit of poking around I found the Lightwindow plugin. It was really easy to add, used data that already existed (e.g. hrefs and classes), and worked the first time. That’s what I expect out of a Javascript library.
How it works together
- When a user requests the Start Page the Page Controller gets instantiated.
- Because it doesn’t have a cmd parameter it will fire the display method in the BLL class by default.
- The display method gets all of the links from the DAL and formats them with the proper Lightwindow classes.
- The formatted links are made available in a variable which is then used to display the data on the page.
The only code on the page is to include the PageController class, instantiate it, and echo any errors and HTML to the page.
A similar process is followed when the user adds and deletes a link. The difference is that this time a cmd parameter would be passed to the controller. The data would then be validated and processed accordingly.
Conclusion
The use of a text database makes it easy to get running on a shared host without the trouble of setting up a MySql database. By using the MVC design pattern and Object Oriented PHP I was able to create a rather robust little Start Page that is ripe for all sorts of creative additions. It is easy to envision support for notes, widgets, categories, multiple layouts, in-place editing, ordering and more.