racoon/router

A basic extension of nikic/fast-route.

1.0.1 2016-06-14 10:07 UTC

This package is auto-updated.

Last update: 2024-12-15 07:19:58 UTC


README

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

Routing

Racoon uses nikic/fast-route to deal with routing.

Defining where routes are stored

Routes need to be added in a routes file, which should be added to the Router, or defined in an anonymous function passed to the router.

$router->addRouteFile('/path/to/some_routes.php');

// Tells the router you are done adding routes so as it can process them.
$router->init();

If you want to store routes in multiple locations you can do it as follows.

$router
    ->addRouteFile('/path/to/some_routes.php')
    ->addRouteFile('/path/to/more_routes.php')
    ->addRouteFile('/path/to/even_more_routes.php');

// Tells the router you are done adding routes so as it can process them.
$router->init();

If you define multiple route locations, they will be included/added in the same order as you define them.

To define routes without storing them in a separate file you can do the following.

$router->addRouteCallable(function($r) {
    // Define your routes here as if they were in another file.
    // $r->addRoute(), $r->addGroup(), etc are all available.
});

Setting up routes

Inside one of the route files that have been added to the router you need to define your routes in the following format.

$httpRequestMethod = ['GET', 'POST'];
$requestUri = '/users/list';
$handlerString = '\\MyApp\\Users@list';
$r->addRoute($httpRequestMethod, $requestUri, $handlerString);

Route Groups

To make it easier to set up long urls multiple times you can use groups. The following code blocks give the same result.

$r->addRoute(['GET', 'POST'], '/users/list', '\\MyApp\\Users@list');
$r->addRoute(['GET', 'POST'], '/users/get', '\\MyApp\\Users@get');
$r->addRoute(['GET', 'POST'], '/users/update', '\\MyApp\\Users@update');
$r->addRoute(['GET', 'POST'], '/users/delete', '\\MyApp\\Users@delete');
$r->addGroup('/users', function () {
    $r->addRoute(['GET', 'POST'], '/list', '\\MyApp\\Users@list');
    $r->addRoute(['GET', 'POST'], '/get', '\\MyApp\\Users@get');
    $r->addRoute(['GET', 'POST'], '/update', '\\MyApp\\Users@update');
    $r->addRoute(['GET', 'POST'], '/delete', '\\MyApp\\Users@delete');
});

You can also use sub-groups. The following code blocks give the same result.

$r->addRoute(['GET', 'POST'], '/some/long/url/do-something', '\\MyApp\\Users@list');
$r->addGroup('/some', function ($r) {
    $r->addGroup('/long', function ($r) {
        $r->addGroup('/url', function ($r) {
            $r->addRoute(['GET', 'POST'], '/do-something', '\\MyApp\\Users@list');
        });
    });
});

HTTP Request Method

The HTTP Request Method(s) that the route should match. This can be any HTTP request type such as GET or POST.

Can be a string or an array of strings.

Request URI

The request URI that the route should match.

You can define the request URI in multiple ways.

'/users/list'
'/users/get/{userId}'
'/users/get/{userId:\d+}'

For more information see the FastRoute Route Docs

Any wildcards/placeholders defined here will be passed into the Controller/Handler method.

Handler String

The handler string defines the class and method that should be executed should the current request match a route.

The required format is \MyApp\Users@list where \MyApp\Users is the full class name including the namespace, and list is the method inside of that class which you want to be executed.