You are currently browsing the category archive for the ‘website’ category.

Framework/Schmamework
Recently I had to build a new website that required:

  • Mainly static pages – but with some dynamically generated elements
  • RESTful URI’s for SEO friendly links
  • Easy to implement and maintain

Pure HTML was out, so my choices were to either roll a completely custom solution or to employ a pre-defined framework. However years ago I started to roll my own custom solution for an internal website I was writing and after getting half way through it I realized that I was well down the path of creating a general purpose web framework. So even my custom solution would end up looking like a framework – and given my druthers I’m not in favor of reinventing the wheel unless I have a really really good reason to do so.

Now my ISP supports PHP (and Perl) and not Python and I have some PHP experience so it’s a no brainer to pick some sort of light weight PHP web framework. A quick google search tuned up a comparison of 39 PHP web frameworks:

Comparison of web application frameworks – PHP

Reading over the frameworks marked as “light weight” left me unimpressed. I really wanted something that so simple to use that I didn’t even notice it was there. In fact all I really wanted was something that did the routing from a RESTful URI to a specific script that generated the pages I wanted to create.

So after even more Google searching I managed to stumble upon the Slim framework, which seemed to suit my needs.

Slim is the new black
The Slim framework (www.slimframework.com) is an extremely light weight PHP based web framework. Once you define the index.php file, the only thing the has to be configured is a folder that contains the web page templates (and even then you don’t need to put anything in there!), and links from URI routes to scripts that generate the pages. It is as if the Slim framework was written specifically to suit my requirements!

Quoting from the Slim website:

What began as a weekend project is now a simple yet powerful PHP 5 framework to create RESTful web applications. The Slim micro framework is everything you need and nothing you don’t.

In fact my entire index.php file amounts to:

// The framework
require_once 'SlimPHP/Slim.php';

// Get a new App
$app = new Slim();

// Main Site
$app->get('/', 'indexRoute');
$app->map('/contact/', 'indexContact' )->via('GET', 'POST');
$app->get('/about/', 'aboutRoute');

// Subpage
$app->get('/notchflow/', 'notchFlowRoute');
$app->map('/notchflow/contact/', 'notchFlowContact' )->via('GET', 'POST');

// Set up the not found function
$app->notFound('ErrorFunction404');

// Run the application
$app->run();

(NOTE that my ISP only currently supports PHP v5.2, but Slim is written to support the latest PHP versions. Because of this I have to use Slim’s 5.2 compatible syntax.)

The above code defines routes to five explicit web pages that are implemented by custom PHP functions that I have written:

http://www.example.com/ is generated by the indexRoute() function.

http://www.example.com/contact/ is generated by the indexContact() function, and the same function responds to both the HTTP GET and POST requests.

http://www.example.com/about/ is generated by the aboutRoute() function.

http://www.example.com/notchflow/ is generated by the notchFlowRoute() function.

http://www.example.com/notchflow/contact/ is generated by the notchFlowContact() function (which also responds to both GET and POST.)

Any other URI will get redirected to a page generated by the ErrorFunction404() function.

Under the hood
The PHP functions that generate the actual web pages are free to do whatever they want. In my case I used the functions to initialize some data, that is incorporated into XHTML files by the Slim render() function

For example, the aboutRoute() function is basically:

function aboutRoute() {
 $app = Slim::getInstance();

 $options = array(
  'metaData' => $metaData,
  'favicon' => '/favicon.ico',
  'styleSheets' => array( array('/css/siteScreen.css','text/css','screen')),
  'scripts' => array(),
  'siteName' => $siteName,
  'companyName' => 'Joalah Apps',
  'productName' => '',
  'menuItems' => $menuItems,
  );

  $app->render('about.xhtml', $options);
}

Where the about.xhtml file is like:

<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta name="robots" content="all" />
  <?php echo metaIncludes($this->data['metaData']); ?>
  <?php echo faviconInclude($this->data['favicon']); ?> 
  <?php echo cssIncludes($this->data['styleSheets']); ?>
  <?php echo scriptIncludes($this->data['scripts']); ?>
  <title>About - <?php echo $this->data['companyName']; ?></title>
 </head>
 <body>
  <div id="pageHeader">
   <?php echo pageHeaderInclude('Joalah Apps', 'Apps you never knew you needed!'); ?>
  </div>
  <div id="pageNavigation">
   <?php echo menuIncludes($this->data['menuItems']); ?>
  </div>
  <div id="pageBody">
   <div class="About">
    <p>This is the home of <?php echo $this->data['companyName']; ?> - the repository of apps from Joalah Designs LLC</p>
    <div id="bragList">
     <ul>
      <li>Apps that are crafted with love and care.</li>
      <li>Apps that you never knew you wanted until after you saw them.</li>
      <li>Apps that will make you the envy of your peers.</li>
      <li>Apps that will make you attractive to the opposite/same sex.</li>
      <li>Apps that .. well you get the idea.</li>
     </ul>
    </div>
    <div>
     <p>Joalah Apps are produced by <a href="http://www.JoalahDesigns.com">Joalah Designs LLC</a></p>
     <p>And you can "Read all about it!" at the mandatory <a href="https://blog.joalahdesigns.com/">Joalah Design's blog</a></p>
    </div>
   </div>
  </div>
  <div id="pageFooter">
   <?php echo pageFooterInclude(); ?>
  </div>
 </body>
</html>

Doing things this way allows me to generalize my XHTML files and write PHP helper methods that render specific features – thus embodying the DRY principle.

Concluding thus
So the Slim framework stays out of my way, yet gives me the power to write websites as complex as I need. Though with all its power its not going to challenge hugely complex frameworks like Zend anytime soon – and nor should it compete on that level. (In fact one of the reasons I went hunting down another framework was because I do have experience with Zend and I could see how much overkill it would be to use Zend for my simple website.)

if anything I have also only used a fraction of what Slim has to offer. I’m pretty sure that Slim will be at the top of the list of frameworks to consider for the next website that I have to build.

There she blows!
Finally .. here is the link to the actual website powered by Slim: www.JoalahApps.com

Enjoy!
Peter