KScript docs
EN

session fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.tokenstringThe session's CSRF token, which forms send back as _token. Same as @csrf_token().

From KScript.Auth

These need <import package="KScript.Auth" />import KScript.Auth. See KScript.Auth.

#session->all() → map KScript.Auth

Every value in the session, as a map.
Example
<echo>{ $session->all() }</echo>
echo $session->all()

#session->clear() → number KScript.Auth

Removes every value, keeping the session (and who is signed in). Returns how many there were.
Example
<do>$session->clear()</do>
$session->clear()

#session->flash(key, value) → any KScript.Auth

Keeps a value for this request and the next one only: a message to show after a redirect, or the form's input to fill it in again.

Arguments can be named: ->flash(…, value: …).

Example
<do>$session->flash('status', 'Your profile was saved.')</do>
$session->flash('status', 'Your profile was saved.')

#session->get(key, default?) → any KScript.Auth

The value kept under a key, or default (null).

Arguments can be named: ->get(…, default: …).

Example
<echo>{ $session->get('cart', default: []) }</echo>
echo $session->get('cart', default: [])

#session->has(key) → bool KScript.Auth

True when a value is kept under the key.
Example
<if test="$session->has('cart')"><echo>You have a cart</echo></if>
if $session->has('cart') {
  echo 'You have a cart'
}

#session->pull(key, default?) → any KScript.Auth

The value kept under a key, which is then removed: a one-time value such as a code. default (null) when it isn't there.

Arguments can be named: ->pull(…, default: …).

Example
<let name="code" value="$session->pull('login_code')" />
$code = $session->pull('login_code')

#session->regenerate() KScript.Auth

Gives the session a new id, keeping its values. @login() does this for you.
Example
<do>$session->regenerate()</do>
$session->regenerate()

#session->remove(key) → bool KScript.Auth

Removes a value. True when it was there.
Example
<do>$session->remove('cart')</do>
$session->remove('cart')

#session->set(key, value) → any KScript.Auth

Keeps a value in the session, for this request and the ones after it. It starts a session (and sends its cookie) if there isn't one.

Arguments can be named: ->set(…, value: …).

Example
<do>$session->set('theme', 'dark')</do>
$session->set('theme', 'dark')

Methods from any