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.
I used the Model-view-controller design pattern in the Start Page which started by defining three main classes:
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.
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:
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.
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.
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.