Terry's Blog Engine Version 3.1.0
Economy of implementation

A Unified Context Layer

In this application I have stuck with a more traditional request-response service model rather than moving to a full MVC framework and model, but I wanted to provide a unified and simple access toolset for the context of any request that a given script is implementing. The AppContext class provides this blog/user/page context for the application, that is the (quasi) static configuration within which script must respond to this particular web request. This context is derived from three components:

A key design objective here to make referencing such context parameters simple-to-code and robust. For example, the AppContext::__get() method overloads the context property access and this handles the isset() or htmlentities() scrubbing issues, so the application code can simply and safely accessing the URI parameter page by the code $cxt->page without this wrapper logic. For more details see AppContext.

A Unified Database Layer

The standard mysqli class requires 4-6 line code patterns for most access functions, and if the parameters are untrusted even more are required to prevent injection attacks. Why use ten lines of code when one will do? It is just as efficient, but is easier to understand with less chance of making an error. The AppDB class extends the standard mysqli interface, and provide a set of methods to achieve this for 95% queries. AppDB::declareFunction() is used during page initialisation to declare query functions to be used in that page, so for example in class searchPage:

        $this->db->declareFunction( array(
'searchKeyword'     => "Set=SELECT id, date, title FROM :articles 
                            WHERE keywords LIKE '%#1%' ORDER BY date DESC",
'searchQuestion'    => "Set=SELECT id, date, title FROM :articles 
                            WHERE MATCH (title,keywords,details) AGAINST ('#1' IN BOOLEAN MODE) 
                            ORDER BY date DESC",
        ) );

so the result for a question is retrieved by a simple method call:

        $matches = $this->db->searchQuestion( $question );

Again any arguments are scrubbed in the Database Layer to avoid injection attacks, so the application does not need to embed this code. For more details see AppDB.

A Unified Template Layer

Separating the business logic from its presentation makes a lot of sense and simplifies the structure of the application. Using a templating engine such as Smarty achieves this and considerably simplifies the application code. However as I describe in class TemplateBuilder, I have built and use my own engine. Why do this if Smarty a field leader and can achieve my economy goals? My answer is that my engine provides 90% of what Smarty could possibly offer in this usecase, and possibly 100%, but with less than 5% of the code base and more to the point — materially faster in a shared hosting environment. For more details on the templating engine see class AppContext. For the efficiency discussion see My Templating engine.

 All Classes Files Functions Variables