예제 #1
0
function routeRequest(Response $response, Request $request)
{
    $routesDefinitionsCallback = function (RouteCollector $r) {
        foreach (require __DIR__ . "/routes.php" as $route) {
            $r->addRoute($route[0], $route[1], $route[2]);
        }
    };
    $dispatcher = \FastRoute\simpleDispatcher($routesDefinitionsCallback);
    $routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getPath());
    switch ($routeInfo[0]) {
        case Dispatcher::NOT_FOUND:
            $response->setStatusCode(404);
            $response->setContent('404 - not found');
            break;
        case Dispatcher::METHOD_NOT_ALLOWED:
            $response->setStatusCode(403);
            $response->setContent('403 - not allowed');
            break;
        case Dispatcher::FOUND:
            $response->setStatusCode(200);
            $className = $routeInfo[1][0];
            $method = $routeInfo[1][1];
            $vars = $routeInfo[2];
            return new Step("{$className}::{$method}", InjectionParams::fromRouteParams($vars));
    }
    return new step(function () {
        echo "something went wrong";
    });
}