KScript documentazione
IT

Lingua

Tradotto automaticamente dall’inglese, che è la versione originale.

Route e API

Risposte

@response(), @view() e @redirect() forniscono un valore di risposta, e il middleware ne riceve uno da $next($request). Puoi leggerlo e modificarlo:

  • Leggilo con .status, .body, .type, .headers, ->header(name), .is_ok e ->convert(from: json).
  • Modificalo con ->with_status(code), ->with_header(name, value), ->with_type(type), ->with_etag(value) e ->with_cache(directives). Ognuno restituisce la risposta, quindi le chiamate possono essere concatenate.
  • ->with_cookie(name, value) imposta un cookie, e ->without_cookie(name) ne elimina uno. Un cookie è HttpOnly e SameSite=Lax se non indichi altro, Secure su https, e dura finché il browser non viene chiuso, a meno che expires: non indichi per quanto tempo: ->with_cookie('theme', 'dark', expires: 30d, not http_only). Ci sono anche path:, domain:, secure e same_site: lax|strict|none.
  • ->with_header('Set-Cookie', ...) aggiunge un header cookie grezzo ogni volta che viene chiamato, invece di sostituire l'ultimo, e ogni cookie viene inviato come un header Set-Cookie a sé.
  • @response($answer) dato una risposta la restituisce così com'è, con il suo stato, e @response($answer, status: 410) ne cambia lo stato.

kscript serve invia Server: kscript sulle risposte HTTP. Per inviare un tuo valore per una route, usa ->with_header('Server', 'my-app').