Exemplo n.º 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";
    });
}
Exemplo n.º 2
0
 /**
  * Delete a single person based on the ID
  *
  * URI: /delete
  *
  */
 public function delete()
 {
     if ($this->request->getParameter('csrf') === $this->csrf->getCurrentToken()) {
         try {
             $this->people->deleteById((int) $this->request->getParameter('id'));
             $message = 'success';
             $response = 200;
         } catch (\Exception $e) {
             $message = 'failure';
             $response = 500;
         }
     } else {
         $message = 'failure';
         $response = 500;
     }
     $template_vars = array('json' => json_encode(array('message' => $message)));
     $html = $this->renderer->render('page/data.php', $template_vars);
     $this->response->setStatusCode($response);
     $this->response->setContent($html);
 }