KScript documentazione
IT

Lingua

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

Campi e metodi di http_response

Risposte prodotte da controller e middleware: @response(), @view(), @redirect().

Un campo è un dato semplice sul valore, letto con un punto e senza parentesi: $value.name. Un metodo richiede argomenti o esegue un'operazione, e si chiama con ->: $value->name(arguments), oppure passando il valore con la pipe: $value |> name(arguments).

Campi

CampoTipoDescrizione
.bodystringIl corpo come testo.
.headersmapLe intestazioni come mappa.
.is_okboolVero quando lo stato è 2xx.
.statusnumberIl codice di stato HTTP, come 200 o 404.
.typestringIl Content-Type.

Da KScript.Http

Questi richiedono <import package="KScript.Http" />import KScript.Http. Vedi KScript.Http.

#http_response->header(name) → string KScript.Http

Un'intestazione (oppure null). Per Set-Cookie impostato più volte, la prima; .headers le ha tutte.
Esempio
<let name="response" value="@redirect('/login')" />
<echo>{ $response.status } { $response->header('Location') }</echo>
$response = @redirect('/login')
echo "{ $response.status } { $response->header('Location') }"

#http_response->with_cache(directives) → http_response KScript.Http

Imposta Cache-Control: ->with_cache('public, max-age=300'), oppure un numero di secondi: ->with_cache(300).
Esempio
<fn name="index" params="request">
  <return value="@view('posts/index')->with_cache(300)" />
</fn>
fn index(request) {
  return @view('posts/index')->with_cache(300)
}

#http_response->with_cookie(name, value, expires?: duration, path?, domain?, http_only?: bool, secure?: bool, same_site?: lax|strict|none) → http_response KScript.Http

Imposta un cookie e restituisce la risposta. Il cookie è HttpOnly e SameSite=Lax salvo diversa indicazione, Secure su https, e dura per la sessione del browser a meno che non sia indicato expires: (ad esempio 30d).
ParametroAccetta
name obbligatorioqualsiasi valore
value obbligatorioqualsiasi valore
expires facoltativodurata
path facoltativoqualsiasi valore
domain facoltativoqualsiasi valore
http_only facoltativoun flag: http_only oppure not http_only
secure facoltativoun flag: secure oppure not secure
same_site facoltativouna di queste parole: lax|strict|none

Gli argomenti possono essere nominati: ->with_cookie(…, value: …, expires: …).

Esempio
<fn name="remember_theme" params="request">
  <return value="@redirect('/')->with_cookie('theme', 'dark', expires: 30d)" />
</fn>
fn remember_theme(request) {
  return @redirect('/')->with_cookie('theme', 'dark', expires: 30d)
}

#http_response->with_etag(value?, weak?: bool) → http_response KScript.Http

Imposta l'ETag: un hash del valore, oppure del corpo se non ne è presente uno. Aggiungi il middleware etag (o etag="true") per rispondere a If-None-Match con 304.
ParametroAccetta
value opzionalequalsiasi valore
weak facoltativoun flag: weak oppure not weak

Gli argomenti possono essere nominati: ->with_etag(…, weak: …).

Esempio
<fn name="show" params="request">
  <return value="@response({ id: 1, title: 'Hello' })->with_etag()" />
</fn>
fn show(request) {
  return @response({ id: 1, title: 'Hello' })->with_etag()
}

#http_response->with_header(name, value) → http_response KScript.Http

Imposta un header e restituisce la risposta: $next($request)->with_header('X-Frame-Options', 'DENY'). Set-Cookie aggiunge ogni volta un cookie invece di sostituire l'ultimo.

Gli argomenti possono essere nominati: ->with_header(…, value: …).

Esempio
<fn name="secure_headers" params="request, next">
  <return value="$next($request)->with_header('X-Frame-Options', 'DENY')" />
</fn>
fn secure_headers(request, next) {
  return $next($request)->with_header('X-Frame-Options', 'DENY')
}

#http_response->with_status(code) → http_response KScript.Http

Cambia il codice di stato e restituisce la risposta.
Esempio
<fn name="store" params="request">
  <return value="@response({ saved: true })->with_status(201)" />
</fn>
fn store(request) {
  return @response({ saved: true })->with_status(201)
}

#http_response->with_type(type) → http_response KScript.Http

Cambia il Content-Type e restituisce la risposta.
Esempio
<fn name="export" params="request">
  <return value="@response('name,email')->with_type('text/csv')" />
</fn>
fn export(request) {
  return @response('name,email')->with_type('text/csv')
}

#http_response->without_cookie(name, path?, domain?) → http_response KScript.Http

Dice al browser di eliminare un cookie e restituisce la risposta. Indica il percorso e il dominio con cui è stato impostato.

Gli argomenti possono essere nominati: ->without_cookie(…, path: …, domain: …).

Esempio
<fn name="forget_theme" params="request">
  <return value="@redirect('/')->without_cookie('theme')" />
</fn>
fn forget_theme(request) {
  return @redirect('/')->without_cookie('theme')
}

Metodi da any