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 →

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.