What are some recommended resources or libraries for efficient PHP routing solutions?

When working on a PHP project, having efficient routing solutions is crucial for handling different URL patterns and directing users to the appropriate pages or actions. One recommended library for PHP routing is FastRoute, which is known for its speed and flexibility in defining routes. Another popular option is Symfony Routing Component, which provides a powerful routing system with features like route parameters and route matching.

// Using FastRoute for efficient PHP routing
require 'vendor/autoload.php';

$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
    $r->addRoute('POST', '/user', 'create_user_handler');
    $r->addRoute('PUT', '/user/{id:\d+}', 'update_user_handler');
});

$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];

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

switch ($routeInfo[0]) {
    case FastRoute\Dispatcher::NOT_FOUND:
        // handle 404 Not Found
        break;
    case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
        // handle 405 Method Not Allowed
        break;
    case FastRoute\Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        // Call the handler with parameters
        break;
}