PHP code example of fastd / routing

1. Go to this page and download the library: Download fastd/routing library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fastd / routing example snippets


use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/', function () {
    return 'hello world';
});

$route = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), []);

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function ($name) {
    return 'hello ' . $name;
});

$route = $collection->match(new ServerRequest('GET', '/foo')); // \FastD\Routing\Route

echo call_user_func_array($route->getCallback(), $route->getParameters());

$collection = new FastD\Routing\RouteCollection();
$collection->get('/', function () {
    return 'hello GET';
});
$collection->post('/', function () {
    return 'hello POST';
});
$response = $collection->dispatch('GET', '/'); // hello GET
$response = $collection->dispatch('POST', '/'); // hello POST

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/{name}', function () {
    return 'get1';
});

$collection->addRoute('GET', '/', function () {
    return 'get2';
});

$route = $collection->match(new ServerRequest('GET', '/abc')); // \FastD\Routing\Route
$route2 = $collection->match(new ServerRequest('GET', '/')); // \FastD\Routing\Route
echo call_user_func_array($route->getCallback(), $route->getParameters());
echo call_user_func_array($route2->getCallback(), $route2->getParameters());

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->group('/v1', function (RouteCollection $collection) {
    $collection->addRoute('GET', '/{name}', function () {
        return 'get1';
    });
});

$route = $collection->match(new ServerRequest('GET', '/v1/abc'));

echo call_user_func_array($route->getCallback(), $route->getParameters());

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function ($path) {
    return $path;
});

$route = $collection->match(new ServerRequest('GET', '/api/abc'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /abc

$route = $collection->match(new ServerRequest('GET', '/api/cba'));
echo call_user_func_array($route->getCallback(), $route->getParameters()); // /cba

use FastD\Http\ServerRequest;
use FastD\Routing\RouteCollection;

$collection = new RouteCollection();

$collection->addRoute('GET', '/api/*', function (ServerRequest $request) {
    return 'hello';
});

$dispatcher = new \FastD\Routing\RouteDispatcher($collection);

$response = $dispatcher->dispatch(new ServerRequest('GET', '/api/abc'));

echo $response->getBody();