Marg — A Simple Request Router written in PHP

Vaidik Kapoor
Nov 10, 2012 · 7 min read

The Code

So lets just dive into some code:

<?php

include 'marg/marg.php';

$routes = array(
'/' => 'home',
);

function home() {
echo 'Hello World!';
}

Marg::run($routes);

?>
<?php

include 'marg/marg.php';

$routes = array(
'/' => 'home',
);

function home() {

echo '<h1>Hello World!</h1>';

global $request;

if ($request->verb == 'GET') {
echo 'This is a GET request.';
} else {
header('HTTP/1.1 405 Method Not Allowed');
echo 'Method Not Allowed';
}
}

Marg::run($routes);

?>
<?php

function home() {
echo '<h1>Hello World!</h1>';

global $request;

if ($request->verb == 'GET') {
echo 'This is a GET request.';
} else {
raise('405');
}
}

function raise_405() {
header('HTTP/1.1 405 Method Not Allowed');
echo '<h1>405: Method Not Allowed</h1>';
}

?>
<?php

include 'marg/marg.php';

$routes = array(
'/' => array('home', array('GET', 'POST')),
);

function home() {
echo 'Hello World!';
}

Marg::run($routes);

?>
<?php

$routes = array(
'/' => array(
'controller' => 'home',
'methods' => array('GET', 'POST')
),
);

?>
<?php

include 'marg/marg.php';

$routes = array(
'/' => array('home', array('GET', 'POST')),
'/classes' => 'ClassesExample',
);

function home() {
...
}

class ClassesExample {
public function get() {
echo '<h1>This is an example of how to use classes with [Marg][MARG].</h1>';
echo '<p>This is a GET request.</p>';
}

public function post() {
echo '<h1>This is an example of how to use classes with [Marg][MARG].</h1>';
echo '<p>This is a POST request.</p>';
}
};

Marg::run($routes);

?>

setUp and tearDown

Marg also allows you to define setUp and tearDown for incoming requests at two different levels. To define a setUp and a tearDown for all the incoming requests i.e. the global level, this is what you’d do:

<?php

include 'marg/marg.php';

$routes = array(
'/' => array('home', array('GET', 'POST')),
);

function home() {
echo 'Hello World!';
}

Marg::run($routes);
Marg::addSetUp(function () { echo '<html><head><title>Marg Examples</title></head><body>'; });
Marg::addTearDown(function () { echo '</body></html>'; });

?>
<?php

class ClassesExample {
public function setUp() {
echo '<h1>This is an example of how to use classes with [Marg][MARG].</h1>';
echo '<center>';
}

public function tearDown() {
echo '</center>';
}

public function get() {
echo '<p>This is a GET request.</p>';
}

public function post() {
echo '<h1>This is an example of how to use classes with [Marg][MARG].</h1>';
echo '<p>This is a POST request.</p>';
}
};

?>

Responses

You must have noticed that Marg does not provide any way of simplifying how you generate your responses. Well, there can be a lot of things that I could have done on that front but that would have been really a lot of opiniated design decisions that I did not want to enforce onto others and myself as well because I do tend to change how I write my applications with time and experience. So how you handle your responses depends entirely on you. You may want to use a templating engine like Smarty or may be you want to write a custom response generation engine yourself. Its up to you!

How and why?

While I was interning at Wingify (the creators of Visual Website Optimizer, check it out in case you don’t know what it is ‘coz its awesome!), one of the projects I was working on was development of a REST API for Visual Website Optimizer (VWO). I hadn’t worked on a dedicated REST API before this, mostly because there was never a need to do the same. But I’d always think of working on one for a project, just didn’t know that it would be VWO. Anyways, I wanted to get everything right and so I started to read about API design and best practices. While doing this, one good thing that happened with me was that I got in the habbit of reading specs. I read many parts of the HTTP 1.1 spec many times. I read docs of many web services’ APIs like Github’s, Facebook’s, Google Data Protocol’s, and many more. And, I got to learn a great deal of things, how they are done, why they are done, etc.

Feedback

Marg was more of a weekend project. I have added code to it but haven’t been able to really devote time to it. I am sure it has some glitches. I’d be really happy to have some feedback. So, please take out some time and let me know how you feel about it.


Vaidik Kapoor

Thoughts on software engineering and technology

Vaidik Kapoor

Written by

Software Engineer, Building Tech at Grofers

Vaidik Kapoor

Thoughts on software engineering and technology