Routing with Macaw

Published on 4th Mar, 2014

Earlier in my PHP days, when I was still learning and rarely understood how things worked, I would always build my custom applications off of an existing platform. Joomla!, Wordpress, Codeigniter, I even spent a little time with Kohana. All of these are great platforms, but they were almost always overkill for what I needed. This resulted in larger overheads in speed and storage, more of a learning curve, and an application that was often constrained and over-complicated. Thus, when I start to write my own application these days, I turn to a few useful tools. I'll be writing several posts discussing some of these.

Today, I wanted to talk about Routing. Routing was a magic word to me in the early days. From context I implied that it had something to do with getting content and loading the various screens I was coding. It's actually not a difficult concept, its implementation can be quite complicated, but the overall idea is to take a URL and parse out a path of includes to load the necessary files or to call specific functions.

This can be done a number of ways. Apache rewrites, regular expressions, etc. But the end goal is to convert a URL into something that our application can use to serve up the right stuff.

I ran across this little gem: Macaw.

Macaw makes routing SUPER simple (and fun!). First off, it uses some Apache .htaccess magic to let the server know that the URL's don't actually point to a real file. For instance, if I were to type in http://mysite.com/test/folder/test.html it would typically look for a file in a similar directory structure i.e. /server/mysite/test/folder/test.html.

But Macaw operates a bit differently. Here is the .htaccess file that comes with it:

   Options -indexes
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 index.php
    </IfModule>

What this does is route things into index.php so that Macaw can handle the routing.

So for example. Say I have a basic application that returns a list of names. To access this, I want people to go to http://mysite/names. Macaw gives me the ability to set up a simple router for this:

    include 'macaw.php'

    Macaw::get('/names', function() {
        $names = myDatabaseFunction();
        print_r($names);
    });

Simple as that! But wait, it gets better!

Say I also want to list restaurants. I could, of course, write another Macaw::get() statement. But as my application grows, I'll want to be a bit more efficient. I could set up a function that grabs a template file, and then use Macaw to pass in the correct view.

    include 'macaw.php'

    function includeTemplate($view){
        include $view . 'html.php';
    }

    Macaw::get('/(:any)', function() {
        $view = explode("/", $slug)[1];
        if($view == "")
            return;
        $views = array("names", "restaurants");
        if(in_array($view, $views)){
            includeTemplate($view);
        };
    });

This is, of course, very basic and not recommended for production but it gives you the basis for building a fantastic router. What I loved about it was how quick it was to learn. Granted I had a simple application, but my router was ready in about half an hour of coding. One could also utilize this to implement PHP's call_user_func() to load dynamic classes.

Check it out. Five minutes of your time and you have a fantastic router in your toolbox. It's also super small and lightweight so take a few extra minutes to dig in and see how it works.

Happy Coding!

Richard

This article is my 24th oldest. It is 569 words long

comments powered by Disqus