Twiny-Framework: the framework small enough to tweet

Posted: 2009-12-15
Category: PHP

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']?:'Home';
if(!@include"c/$c.php")die('fail');
$m=methodexists($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

Gravatar
Phil

2009-12-18

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

2009-12-18

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

Gravatar
Jeremy Lindblom

2009-12-18

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
Jelle

2010-07-29

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
Shay

2011-04-19

Awesomeness in 90 chars!

$g=$_GET;$c=@$g['c']?:'Home';$m=@$g['m']?:'index';
require"c/$c.php";
$o=new$c;$o->$m($g);

It work similarly to your solution, short yet very cool!

Thanks for the interesting post.

Gravatar
Alfie Punnoose

2011-07-08

Nice coding. I wish it had SEO friendly URL like that of CI

Gravatar
Iphone

2011-08-18

I have got the entire code of a PHP application, I have setup the database as well. I can run the application and see. How to quickly find out whether it uses any PHP framework or not?[url=http://iphonereleases.com/]iPhone[/url]

Gravatar
Shay

2011-10-28

A newer version of my code, 86 chars :)

$g=$_GET;$c=@$g['c']?:'Home'; require"c/$c.php"; $o=new$c;$o->{@$g['m']?:'index'}($g);

Posting comments after three months has been disabled.