Request routing using @route annotations
PHP HTML
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
docs
lib
tests
Doxyfile
LICENSE
README.md
Request.phar
phpunit.xml

README.md

Usage

See ./docs for the API.

<?php require 'Request.phar';

/**
 * @route /
 */
function showIndex($context) {
    $context->render('index.phtml');
}

/**
 * @route /users
 */
class Users {

    /**
     * @route /:id
     */
    public function showUser($context) {
        $user = $context->id;
        //...
    }

    /**
     * @route
     */
    public function showAllUsers($context) {
        //
    }

    /**
     * @route
     * @method post
     */
    public function createUser($context) {
        //
    }
}

dispatch();

Default dispatch

This is what goes on under the hood - customise if you like.

<?php

function dispatch(/* $args */) {

    $args = func_get_args();
    array_unshift($args, new Request\Context);

    //See if there's some cached routes
    $routes = apc_fetch('routes');
    if (false === $routes) {
        require 'phar://Request.phar/Request/Router.php';
        $router = new Request\Router;
        $routes = $router->getRoutes();
        apc_store('routes', $routes);
    }

    //Dispatch to all functions/methods with a @route annotation
    $dispatcher = new Request\Dispatcher;
    $dispatcher->setRoutes($routes);
    $dispatcher->dispatch($args);

}

Tests & Documentation

To run tests and generate documentation using Doxygen, run

phpunit tests && doxygen