Skip to content

Handling requests

nvs serve runs your entry file once for each request. Each request runs on its own. It has its own copy of every variable and every static property, so one request cannot change a value that another request reads.

A #[Core\Route] attribute on a method connects a path to that method. Core\Router::dispatch finds the route that matches the request and calls its method:

<?nvs
// This program calls the method of the route that matches the request.
class Shop
{
// `#[Core\Route]` connects the path `/products/{id}` to this method.
#[Core\Route(path: '/products/{id}', method: Core\Http\Method::Get)]
#[Core\Access(allow: Core\Audience::Public)]
public function product(uint $id): void
{
// The request is `GET /products/7`, so this prints `product 7`.
echo 'product ', $id, "\n";
}
}
// The gate checks each request before a route method runs, and it writes the error pages.
class Gate implements Core\Router\Gate
{
public function allows(Core\Router\Match $route): bool
{
return $route->accessAs<Core\Audience>() == Core\Audience::Public;
}
// For a path with no route, the status is already 404.
public function refused(Core\Router\Refusal $reason, ?Core\Router\Match $route): void
{
echo "not possible\n";
}
public function failed(?Core\Router\Match $route, ?Throwable $error): void
{
echo "something went wrong\n";
}
}
// `dispatch` finds the route for the request and calls its method.
Core\Router::dispatch(new Gate());
nvs serve (HTTP Server Response)
product 7

The output above is for the request GET /products/7. dispatch converts {id} to the type of the parameter $id, so $id is the number 7.

Gate is a class of your own. Its method allows runs before each route method. Its methods refused and failed write the error pages. For a path that matches no route, dispatch sets the status 404 and calls refused.

You can also call the route methods yourself. Core\Request::route() returns the route that matches the request, or null. Syntax: routes shows both ways.

Core\Request has a method for each part of the request:

  • Core\Request::queryParameter("page") reads a parameter from the address. It returns null if the address has no page.
  • Core\Request::queryParameters("tag") reads a parameter with brackets, such as tag[]=a&tag[]=b, as an array.
  • Core\Request::formFieldsAs<Contact>() reads a form into an instance of your class.
  • Core\Request::jsonAs<Order>() reads a JSON body into an instance of your class.
  • Core\Request::header("accept") reads one header.

Every text value from the request is tainted. Read Tainted input to learn what that means.

echo writes the body. Core\Response::setStatus sets the status code, and Core\Response::setHeader sets a header. The status is 200 when you do not set one. Core\Response::sendJson and Core\Response::redirect write a whole response in one call.

To build a link to another page, use Core\Router::url with the name of the route. It adds the right prefix when the application is mounted under a path such as /shop.

  • Do write all error pages in one gate class, and call Core\Router::dispatch with it.
  • Do give every route a name, and build links from the name with Core\Router::url.
  • Do read a form or a JSON body into a class with Core\Request::formFieldsAs or jsonAs. Each property of the class is then checked.
  • Do set a status for every error response, such as 404 or 422.
  • Don’t keep data for the next request in a static property. Each request starts with fresh values.
  • Don’t build a link by joining strings. The prefix of a mounted application is then missing.

Read more in Syntax: routes and Reference: Core\Request.