sikker/phinatra

Phinatra is a lightweight PHP router inspired by Sinatra, ideal for light or RESTful applications.

1.2.2 2018-12-09 00:36 UTC

This package is not auto-updated.

Last update: 2024-05-06 13:02:54 UTC


README

Phinatra is a lightweight URI router written to be vaguely similar to Sinatra from Ruby. It provides the Controller layer of Model-View-Controller, and can form an MVC application if joined by for instance Doctrine for models and Twig for views.

Example of usage:

<?php

require 'vendor/autoload.php';

use Sikker\Phinatra\Request;
use Sikker\Phinatra\Response;
use Sikker\Phinatra\Router\Router;
use Sikker\Phinatra\Router\RouterException;
use Sikker\Phinatra\Router\Route;
use Sikker\Phinatra\Router\Path;

$path = new Path();
$router = new Router($path);

$router->attach(new Route('/menu/for/tonight', function(Request $request, Response $response){
	$response->setOutput('Spam, egg, sausage and spam');
	return $response;
}));

try {
	$response = $router->route(new Request($path), new Response());
} catch (RouterException $e) {
	$response = new Response();
	$response->setStatusCode(404);
	$response->setOutput( $e->getMessage() );
}

$response->handle();