API routes are endpoints that return JSON data, typically used for REST APIs, mobile apps, or frontend applications.
Basic API Route Definition
API routes are defined in routes/api.php and return a function that accepts a RouteCollector instance:
<? php
use FastRoute\ RouteCollector ;
return function ( RouteCollector $r ) {
// Your routes go here
};
Adding Routes
Simple Routes
Use addRoute() to define routes with specific HTTP methods:
$r -> addRoute ( 'GET' , '/api/test' , 'test_get' );
$r -> addRoute ( 'POST' , '/api/test' , 'test_post' );
$r -> addRoute ( 'GET' , '/api/items' , 'items_get' );
$r -> addRoute ( 'POST' , '/api/items' , 'items_post' );
HTTP Method Shortcuts
FastRoute provides convenient methods for common HTTP verbs:
$r -> get ( '/api/test/{id:\d+}/test/{id2:\d+}' , 'getid' );
$r -> delete ( '/api/test/delete/{id:\d+}' , 'delete' );
$r -> post ( '/api/test/{test:\d+}' , 'postdata' );
The {id:\d+} syntax defines a route parameter that only matches digits. The pattern after the colon is a regular expression.
Route Parameters
Capture dynamic values from the URL using route parameters:
// Single parameter
$r -> delete ( '/api/test/delete/{id:\d+}' , 'delete' );
// Multiple parameters
$r -> get ( '/api/test/{id:\d+}/test/{id2:\d+}' , 'getid' );
// Parameter in POST route
$r -> post ( '/api/test/{test:\d+}' , 'postdata' );
The handler function receives parameters as an array:
function getid ( $var ) {
if ( is_array ( $var )) {
$id = $var [ 'id' ];
$id2 = $var [ 'id2' ];
}
echo "given id: " . $id ;
echo "given id: " . $id2 ;
}
Route Groups
Group related routes with a common prefix using addGroup():
$r -> addGroup ( '/api/projects' , function ( FastRoute\ RouteCollector $r ) use ( $obj ) {
$r -> get ( '/testing/test' , function () use ( $obj ) {
$obj -> hello ();
});
$r -> get ( "/" , function () use ( $obj ) {
$obj -> display ( 1 );
});
});
$r -> addGroup ( '/api/tasks' , function ( FastRoute\ RouteCollector $r ) use ( $obj ) {
$r -> get ( "/" , function () use ( $obj ) {
$obj -> display ( 2 );
});
});
Route groups are perfect for organizing endpoints by resource or API version.
Using Class-Based Handlers
You can use class methods as handlers by including the class and passing the instance:
include __DIR__ . "/../v1/test.php" ;
$obj = new testing ;
return function ( RouteCollector $r ) {
$obj = new testing ;
$r -> get ( '/api/class' , function () use ( $obj ) {
$obj -> hello ();
});
};
Inline Closure Handlers
For simple logic, you can define handlers directly in the route definition:
$r -> get ( '/' , function () {
require __DIR__ . '/../views/main.php' ;
});
$r -> get ( '/about' , function () {
require __DIR__ . '/../views/about.php' ;
});
The main index.php file automatically sets the appropriate content type based on the route:
if ( strpos ( $uri , '/api/' ) === 0 ) {
header ( 'Content-Type: application/json; charset=utf-8' );
} else {
header ( 'Content-Type: text/html; charset=utf-8' );
}
All routes starting with /api/ will automatically return JSON content type. Make sure your handlers return valid JSON.
Complete Example
Here’s a complete example from the source code:
<? php
use FastRoute\ RouteCollector ;
use FastRoute\ ConfigureRoutes ;
include __DIR__ . "/../v1/test.php" ;
$obj = new testing ;
return function ( RouteCollector $r ) {
$obj = new testing ;
$r -> addGroup ( '/api/projects' , function ( FastRoute\ RouteCollector $r ) use ( $obj ) {
$r -> get ( '/testing/test' , function () use ( $obj ) {
$obj -> hello ();
});
$r -> get ( "/" , function () use ( $obj ) {
$obj -> display ( 1 );
});
});
$r -> addGroup ( '/api/tasks' , function ( FastRoute\ RouteCollector $r ) use ( $obj ) {
$r -> get ( "/" , function () use ( $obj ) {
$obj -> display ( 2 );
});
});
$r -> addRoute ( 'GET' , '/api/test' , 'test_get' );
$r -> addRoute ( 'POST' , '/api/test' , 'test_post' );
$r -> addRoute ( 'GET' , '/api/items' , 'items_get' );
$r -> addRoute ( 'POST' , '/api/items' , 'items_post' );
$r -> addRoute ( 'GET' , '/api/items/1' , 'item_get_1' );
$r -> get ( '/api/test/{id:\d+}/test/{id2:\d+}' , 'getid' );
$r -> delete ( '/api/test/delete/{id:\d+}' , 'delete' );
$r -> post ( '/api/test/{test:\d+}' , 'postdata' );
$r -> get ( '/api/class' , function () use ( $obj ) {
$obj -> hello ();
});
};
Next Steps
Route Handlers Learn about different handler types
Web Routes Create routes that return HTML