The nerdy adventures of...

Phil Sturgeon


My name is Phil Sturgeon and I program stuff in PHP & CodeIgniter.
I write about Linux, Git, jQuery and all sorts of other computery things!

Twiny-Framework: the framework small enough to tweet

Posted in PHP at Dec 15, 2009

Many frameworks say they are lightweight, quick and easy on your server, but none are as lightweight as my new "framework" which is so lightweight it can fit in a Tweet.

$g=$_GET;$c=@$g['c']?$g['c']:'Home';
if(!@include"c/$c.php")die('fail');
$m=method_exists($c,$m=@$g['m'])?$m:'index';
$o=new$c();$o->$m($g);

This idea was not my own, but was inspired by Twitto. During some extreme boredom at work I decided to "one-up" this teeny-tiny framework and improve on it. My framework does this by allowing you to run Controller files and methods in a similar way to CodeIgniter, while Twitto only has the ability to run functions from a single file.

Usage

class Blog
{
    function index()
    {
        echo "This is my blog!";
    }
    function view( $params )
    {
        echo "This is article #".$params['id'];
    }
}

This simple class structure contains a method for each page, defaulting to index. If no controller is set, then it goes to Home.php.

  • index.php - Runs Home->index() in ./c/Home.php
  • index.php?c=Blog - Runs Blog->index() in ./Blog.php
  • index.php?c=Blog&m=view&id=1 - Runs Blog->view($params) and id can be accessed via $params['id']

You can get to that with URL's like index.php?c=Blog&m=view&id=1, but with some mod_rewrite that could be reduced to /Blog/view/id/1 easily enough.

MVC?

Want full MVC? Set an autoload function to check ./m/ and use:

    function view($params)
    {
        include'm/blog_m.php';
        blog_m::get( $params['id'] );
    }

Views would just be another include with direct access to your variables set in the controller.

Summary

As I said this mainly came out of boredom and is not meant for serious use (or serious ridicule), but it is cool to see how much can be done with less than 140 characters of PHP as base. I could do some awesome things with this by taking it up to 300-400 characters, but that would spoil the fun right?

Comments

User comments
  • Gravatar Phil

    Dec 18, 2009

    Nice work. You could shave off a 2 characters by doing new$c; instead of new$c(); and if you're using 5.3, you can shave off another 7 characters by doing $c=$g['c']?:'Home'; instead of $c=@$g['c']?$g['c']:'Home';

  • Gravatar Ellisgl

    Dec 18, 2009

    How about a series of tweets.. use the hash tag for file identification...

  • Gravatar Jeremy Lindblom

    Dec 18, 2009

    Nice! Very clever, and a better solution IMO than Twitto. You could save 2 more characters in your last line by removing the parentheses in your controller instantiation: $o=new$c;

Post a comment