Skip to content

Database-Driven Content Using the Zend Framework

A couple of weeks ago I was asked by a friend for my thoughts regarding the use of the Zend Framework to create a site where the content for some pages could be stored in a database and managed by a simple content-management style administration area. Normally the MVC paradigm works well for web-based applications - probably part of the reason it has become so popular recently - but it does not as easily lend itself to this sort of system. Since the content (effectively the view) is stored in a database, there is very little for the traditional MVC 'view' component to do. Also, there is the problem of URLs. However, with a little thinking about the problem, I came up with a simple demonstration application allowing storage of pages within a database system.

How it works

Putting the system together required several components in order to function correctly. These are outlined below.

  • Controller - DynamicPageController
    This controller has a single action on it, whose function is to take a parameter passed to it (an identifier for a single page) and execute a database operation to retrieve the content. The content is then passed to the view for display.
  • View
    As noted above, the view simply shows the dynamic content from within the database. Since it is written in this way, it allows for the pages to be contained within any layout defined for the site (or module).
  • Database service class
    In the demonstration application, the 'database' is a simple class using arrays for storage. However, in a real application this class would be swapped for something using a RDBMS or other suitable persistant storage medium. It is this class which provides methods for retrieving the content of a page, a list of pages and any other data access methods. In the demonstration application, the data is returned as a collection of arrays - the use of model classes should be considered to produce a more maintainable and polished codebase for a finished product.
  • Route building
    Within the Bootstrap class of the application, once the MVC environment and so forth has been set up, the routes are built and added to the front controller's router. It is this collection of routing rules which allow the application to determine what controller action method should be invoked for a given request, and what parameters (if any) should be passed to the action. In order to allow each dynamic page to be called, a route is built for each one, containing the URI to be used for the page. Each of these routes specify that the DynamicPageController::show method is to be invoked, and each route specifies a different page ID. This is what is passed to the controller and is used to identify which page's content is to be displayed.

Shortcomings & Futher Ideas

Since this was just a simple demonstration application I knocked up within an hour, it has lots of rough edges which could benefit from further work. As mentioned above, the database service class is very crude and is used to just simulate a database using a collection of arrays. This would benefit from using a proper database or other storage medium, and transporting data around the system by encapsulating it in proper model classes instead of simple arrays. This would help maintainability if this idea was to be integrated into a larger solution. The route building logic is also very simple, and despite the fact it works well for a small number of pages I think it could become a significant performance bottleneck with a larger number. One way to mitigate this problem would be to cache the routing data between requests so that the route data does not need to be retrieved and routes built at the beginning of each request. A related issue would still remain - that is the fact there could be a very large number of routes. I am unsure how much of a performance hit this might cause. This could be an area for testing and/or benchmarking. I'm sure there are other areas which could benefit from further tweaking and any comments on the code and general idea are greatfully recieved. With that said, feel free to download the code and use as you like. This code is supplied as a demonstration only with no warranty or guarantees as to its suitability and so on. That said, I hope it helps.

How do I get it?

You can download the code (in a ZIP file) here. This was tested using Zend Framework 1.7.5, but some earlier and all later versions of 1.x should work correctly.

To run the application, simply add the Zend Framework in the [site root]/library/Zend folder and visit the site.