Application

Learn how BitFrame Application stores route data internally and how it can be used

Routes Collection

Application Routes

Routes added via \BitFrame\Application are stored in a \BitFrame\Router\RouteCollectionInterface object and passed along middleware via the http request object. This by itself does not do anything, unless a router (such as \BitFrame\Router\FastRouteRouter) reads it and processes the routes. It was designed this way so that:

  1. You can store the routes via the application object and not have the router hard-coded into the application;
  2. The routes can be passed down via the http request object and can be read by any router that you may use to manage routes.

Creating Routes

Following are all the supported routing methods for use with \BitFrame\Application:

  1. map($method, $path, $handler): \BitFrame\Router\Route
  2. get($path, $handler): \BitFrame\Router\Route
  3. post($path, $handler): \BitFrame\Router\Route
  4. put($path, $handler): \BitFrame\Router\Route
  5. patch($path, $handler): \BitFrame\Router\Route
  6. delete($path, $handler): \BitFrame\Router\Route
  7. head($path, $handler): \BitFrame\Router\Route
  8. options($path, $handler): \BitFrame\Router\Route

The $handler parameter does not necessarily have to be a callback, it could also be a controller class/function name.

Example:
/** Add a route to the map.  */
$app->map(['GET', 'POST'], '/test', function ($request, $response, $next) {
    $response->getBody()->write('Test Page');

    return $response;
});

Using Routes

As mentioned earlier, routes added using the \BitFrame\Application instance by itself does not do anything, unless a router (such as \BitFrame\Router\FastRouteRouter) reads it and processes the routes. They are, in fact, stored in a BitFrame\Router\RouteCollection object and passed down the middleware chain as BitFrame\Router\RouteCollectionInterface attribute. In a middleware, you could read the stored routes on the application object like so:

use BitFrame\Router\RouteCollectionInterface;

// ...

if (! empty($appRoutes = $request->getAttribute(RouteCollectionInterface::class, []))) {
    foreach ($appRoutes->getData() as $route) {
        // $route->getMethods(); e.g. ['GET', 'POST']
        // $route->getPath(); e.g. '/test'
        // $route->getCallable();
    }
}

This would of course have to be inside a method that's going to be handled by the middleware dispatcher as that method would receive the HTTP Request object.

Controllers

You could use a variety of ways to handle routes using either \BitFrame\Application or \BitFrame\Router\FastRouteRouter instance. Follow the how-to handle routes guide for more infortmation.

Route Groups

All routes defined inside a group have a common prefix. For example:

use \BitFrame\Router\RouteCollectionInterface;

$app = new \BitFrame\Application;

$app->group('/admin', function (RouteCollectionInterface $r) {
    function handler($request, $response, $next) {
        $response->getBody()->write('Admin Page');
        return $response;
    }

    $r->get('/do-something', 'handler');
    $r->get('/do-another-thing', 'handler');
    $r->get('/do-something-else', 'handler');
});

Comments

Let us know if you have something to say or add