bramus/router Updates

It’s been 4 months since I released bramus/router, the lightweight and object oriented PHP Router I wrote. Since then a few new features worth mentioning were added.

Subrouting Support

It’s now possible to mount several routes onto a base route. Think of creating a /movies route on which you attach a callable which in its turn adds subroutes onto the base route. The result is that all subroutes will get prefixed with that base route:

// Create a Router
$router = new \Bramus\Router\Router();

// Subrouting
$router->mount('/movies', function() use ($router) {

	// will result in '/movies'
	$router->get('/', function() {
		echo 'movies overview';
	});

	// will result in '/movies'
	$router->post('/', function() {
		echo 'add movie';
	});

	// will result in '/movies/id'
	$router->get('/(\d+)', function($id) {
		echo 'movie id ' . htmlentities($id);
	});

	// will result in '/movies/id'
	$router->put('/(\d+)', function($id) {
		echo 'Update movie id ' . htmlentities($id);
	});

});

// Thunderbirds are go!
$router->run();

You can of course mount any callable you like:

class MoviesController {

	public function __construct($router) {

		// will result in '/movies'
		$router->get('/', function() {
			echo 'movies overview';
		});

		// will result in '/movies'
		$router->post('/', function() {
			echo 'add movie';
		});

		// will result in '/movies/id'
		$router->get('/(\d+)', function($id) {
			echo 'movie id ' . htmlentities($id);
		});

		// will result in '/movies/id'
		$router->put('/(\d+)', function($id) {
			echo 'Update movie id ' . htmlentities($id);
		});

	}

}

$router->mount('/movies', function() use ($router) { new MoviesController($router); });

Support for HEAD requests

bramus/router will now properly respond to HEAD requests, as per RFC2616 (Hypertext Transfer Protocol — HTTP/1.1) Specification:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response. The metainformation contained in the HTTP headers in response to a HEAD request SHOULD be identical to the information sent in response to a GET request.

Internally the request method is overwritten to GET when a HEAD request is made. Output buffering then keeps output from trickling into the response.

Support for X-HTTP-Method-Override

It’s now also possible to use an X-HTTP-Method-Override header to override the HTTP Request Method.

Some HTTP clients can only work with simple GET and POST requests, or sometimes firewalls can spoil all the fun. Although there aren’t any hard standards here, the popular convention is to do a POST request and accept a request header X-HTTP-Method-Override with a string value containing one of PUT, PATCH or DELETE to override the method

bramus/router (GitHub) →
bramus/router on Packagist →
bramus/router Travis CI build status →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.