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:
- You can store the routes via the application object and not have the router hard-coded into the application;
- 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
:
-
map($method, $path, $handler): \BitFrame\Router\Route
-
get($path, $handler): \BitFrame\Router\Route
-
post($path, $handler): \BitFrame\Router\Route
-
put($path, $handler): \BitFrame\Router\Route
-
patch($path, $handler): \BitFrame\Router\Route
-
delete($path, $handler): \BitFrame\Router\Route
-
head($path, $handler): \BitFrame\Router\Route
-
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.
*
* @param array|string $method
* @param string $path
* @param string|callable $handler
*
* @return \BitFrame\Router\Route
*/
$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'); });