So, another in a series of posts where I package up some code I often use into a reusable library, let me introduce a simple PHP library for creating virtual pages.

Virtual pages are pages on a website that are generated in code, and are sent to the client’s browser, but don’t correspond directly to a physical file. This process requires mod_rewrite on Apache, but similar functionality exists in other web servers.

Defining your endpoint

You must specify your endpoint, and then a handling function. This function can be anything callable; functions, methods or an enclosure.

\simple_page_handler\Page::create('my/page/', function($page, array $subpages) {
        // Your page handling code
});

Writing your endpoint handler

You then trigger the handled pages by writing a page handler, and then directing Apache to redirect unhandled requests to this endpoint.

Example endpoint:

try {
        if (!\simple_page_handler\Page::call(\simple_page_handler\Input::get('page'))) {
            \simple_page_handler\Page::set503();
            echo "Something went wrong.";
        }
} catch (Exception $e) {
    echo $e->getMessage();
}

And your redirect code:

# Redirect anything that isn't a real file to our example page handler
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ example_page_handler.php?page=$1 [QSA]

Hopefully this’ll be useful to someone!

» Visit the project on Github...

Over on GitHub I have just open sourced a PHP web and web services framework which I’ve been making use of to build a lot of projects recently.

Initially, it was built for a single project but I’ve ended up using it for many other things, and I thought it might be useful to the Open Source community.

Features

  • Pluggable
  • Light weight
  • Sophisticated events system
  • Abstracted database layer
  • MVC architecture
  • Virtual pages
  • Object/Metadata based data model

Its designed to have much of its functionality carried in plugins, many of which I will release a little bit later just as soon I’ve had the time to tidy them up a bit!

Anywho, its available under the MIT licence and hopefully it’ll be useful to you!

» Github Project Page (Core Plugins, Extra Plugins)