PHP Developer / Blog

February
16th, 2008

The smallest PHP framework you shouldn’t code without

Digg this article · Save to del.icio.us · Stumble it!

This step by step guide will help you get set up with EpiCode (documentation) in under 15 minutes. EpiCode is a framework that maps URLs to static methods. It also has a few utility functions to make templating completely painless. There’s no new templating syntax to learn since PHP does a great job at that already. EpiCode also handles variable scope so you don’t have to worry about mucking up the global namespace.


Step 1
Download the source from http://www.jaisenmathai.com/files/epicode-1.0.tar.gz. The package should contain a php and htdocs directory. Extract these files to your web server such that the htdocs directory is your document root. EpiCode separates your models and views from the controller by placing them outside the web root. This helps ensure your site doesn’t leak files unintentionally.

You’ll need to make sure that your web server has mod_rewrite turned on and allows .htaccess files. Inside the htdocs directory you’ll find an index.php and a .htaccess file. The .htaccess file is pretty simple and has the following rules.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?__route__=$1 [L,QSA]

This will take any request to a file which does not exist and re-route it to index.php. The file path and query string are passed in to index.php. This way you can use GET and POST variables like normal.

Step 2
One of the most important steps is to define your routes. These are URLs which you want your users to be able to visit. These can be anything that’s a valid path (i.e. yoursite.com/mypage, yoursite.com/somefile.html). It’s important to know that these directories or files do not need to exist. EpiCode will take that path and map it to a method you specify. You can specify the mappings as an array $_['routes'] = array([...]). The included sample defines the routes in the index.php file.

$_['routes'] = array(
                ”                => array(’MyClass’, ‘MyMethod’),
                ’sample’          => array(’MyClass’, ‘MyOtherMethod’),
                ‘anypath/source’  => array(’MyClass’, ‘ViewSource’),
              );

This will create 3 pages or URLs. If a user goes to yoursite.com then EpiCode would call MyClass::MyMethod(). The URL yoursite.com/sample would execute MyClass::MyOtherMethod(). Inside of your methods you can do whatever you’d like. Going into details is outside the scope of the EpiCode framework itself. Generally from inside the method I include other class files and do all of the business logic. I mainly use EpiCode::display() and EpiCode::redirect().

Step 3
Now you can actually fill in the methods to add some content and functionality to your site. The details of this step are pretty boring but the methods in the example give you a good place to start.

  class MyClass
  {
    static public function MyMethod()
    {
      echo '

You are looking at the output from MyClass::MyMethod

‘; } static public function MyOtherMethod() { echo ‘

You are looking at the output from MyClass::MyOtherMethod

‘; } static public function ViewSource() { echo ‘

You are looking at the output from MyClass::ViewSource

‘; highlight_file(__FILE__); } }

Conclusion
EpiCode is perfect for developers who want a framework which doesn’t completely define their workflow. You have a great deal of flexibility and EpiCode doesn’t define ways in which you should do common tasks. This is by design as there are more than several ways to tackle each problem. Leave a comment if you have any questions and make sure you check out the documentation.

12 Responses to “The smallest PHP framework you shouldn’t code without”

  1. Framework Says:

    Nice…I’ll try it out.

  2. Jeff Eaton Says:

    This looks like a pretty slick utility. Are there provisions for mapping successive portions of the URL to method parameters?

  3. jaisen Says:

    Jeff,

    You can map /foo to a method and map /foo/bar to another method. It also traverses the url so if you only have the route /foo defined and a user tries to go to /foo/bar then it will load /foo. An example would be http://www.jaisenmathai.com/resume which exists and http://www.jaisenmathai.com/resume/foo/bar which does not exist.

  4. Samuele Says:

    and how to set a route like
    /print/$id ? where id is dinamic?

  5. Jeff Eaton Says:

    Samuele, that’s what I was wondering about. Drupal’s built-in menu router, for example, will do the path-walking that jaisen explains above, but will pass on any unused chunks of the URL as method params. For example:

    http://www.mysite.com/foo gets mapped to $myobj->doFoo().

    If you hit the url http://www.mysite.com/foo/bar, it would call $myobj->doFoo(’bar’);. I’ve found that approach to be pretty effective.

  6. jaisen Says:

    @Samuele,
    The framework doesn’t support that currently. I haven’t come up with a really good way to implement that in a way which compliments the goal of EpiCode (small/fast). Would love input/feedback though. Right now any dynamic variables in the url need to be passed in through the query string and accessed via $_GET.

  7. jaisen Says:

    @Jeff,
    I’m not too familiar with drupal. Does it let you go 3 levels deep? For example http://mysite.com/foo/bar/extra? If so, how does it handle that?

    That is a really interesting idea actually. I just thought that it might be useful to have the first string be the class, second the method…then every additional one would be a parameter passed into that method.

    So, http://mysite.com/foo/bar/1/deleted/jaisen would call foo->bar(1, ‘deleted’, ‘jaisen’).

    Thoughts?

  8. Richard Says:

    I wasn’t able to download the framework. I was wondering how it’s size and speed compared to CodeIgniter (http://www.codeigniter.com/).

  9. jaisen Says:

    @Richard, the link should work now. I’m not really familiar with codeigniter but I had a look and it seems to be interesting. It’s much more of a full featured framework (with plugins/modules and whatnot) than what I describe here. This framework is mean to do the bare minimum and leave the rest up to the developer to implement as they see fit.

  10. satya prakash karan Says:

    I followed this text link: (http://www.jaisenmathai.com/files/epicode-0.1.tar.gz.)
    I got this Error. -
    After much deliberation we have determined that the file you requested contains top secret information which we could not supply to you. Please check with your boss and make sure you have sufficient privileges to view this file.

  11. satya prakash karan Says:

    strange! second time i got the download fies. I will check it another day.

  12. Neo Says:

    http://episuite.jaisenmathai.com/docs/ not working.

Leave a Reply

Captcha
Enter the letters you see above.


About this site:
This is my (Jaisen Mathai) personal site for potential employers who want to see my resume or portfolio. My ideal job would be to work as a PHP developer on a large scale consumer website. My experience is in using PHP, MySQL, Ajax and JSON. I really enjoy creative brainstorming...taking a problem apart and narrowing 100 solutions down to the best one.

Thanks for stopping by. Be sure to drop me a line.