Phil Sturgeon

Web developer, kayaker, outdoors madman and part-time alcoholic.


Twiny-Framework: the framework small enough to tweet

Posted 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.

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 Jelle

    Jul 29, 2010

    Hi Phil,

    Nice job there!
    But I have just one thing i cannot figure out to get working correctly.
    I want to have clean urls with .htaccess but everything I try doesn't seem to work or gives an internal server error.
    Is there any way you can jumpstart me (and ofcourse others) by showing a simple method of doing this?

    Thanks in advance!

  • 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;

  • Gravatar Ellisgl

    Dec 18, 2009

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

  • 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';

Post a comment