Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tharlab/4bfb43a44fef09e4dac253029ebd656d to your computer and use it in GitHub Desktop.
Save tharlab/4bfb43a44fef09e4dac253029ebd656d to your computer and use it in GitHub Desktop.
How to install and use the PHP FastRoute

STEP 1

Create the folder e.g. "/c/www/tests/php-fast-route/" on your machine.

cd /c/www/tests/php-fast-route/
composer init
composer require nikic/fast-route

STEP 2

Create the file "/c/www/tests/php-fast-route/index.php".


STEP 3

Insert the below PHP code.

<?php
require 'vendor/autoload.php';

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    
    $r->get('/', function() {
        echo 'home page goes here';
    });

    $r->addRoute('GET', '/users', 'getUsers');
    $r->addRoute('GET', '/test', 'testFunction');

    $r->get('/books/{id}', function ($args) {
        // Show book identified by $args['id']
        echo "Book #" . $args['id'];
    });

    // {id} must be a number (\d+)
    $r->addRoute('GET', '/user/{id:\d+}', function ($args) {
        echo "User #" . $args['id'];
    });
    // The /{title} suffix is optional2
    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', function ($args) {
        echo "User #" . $args['id'];
        echo "<br>Title: " . $args['title'];
    });
});

// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);

function getUsers()
{
    echo "getUsers function action goes here...";
}

function testFunction()
{
    echo "testFunction action goes here...";
}

$routeInfo = $dispatcher->dispatch($httpMethod, $uri);

switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        die('NOT_FOUND');
        // ... 404 Not Found
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        $allowedMethods = $routeInfo[1];
        // ... 405 Method Not Allowed
        die('Not Allowed');
        break;
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
      
        print $handler($vars);
        break;
}

STEP 4

Write the below command in the terminal to start the server.

php -S localhost:81

STEP 5

Navigate to the pages below to see how it works:

http://localhost:81/
http://localhost:81/users
http://localhost:81/test
http://localhost:81/books/123
http://localhost:81/user/357
http://localhost:81/articles/987/this-is-a-title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment