Sessions


Monti provides a thin static wrapper around PHP's native session handling: the \Session class. It is a convenience layer on top of session_start()/$_SESSION.

Starting and stopping

The session is started automatically during the framework bootstrap, you don't need to call this yourself.

\Session::start(); // calls session_start() if no session is active
\Session::stop();  // destroys the session
\Session::status(); // "none" | "active" | "disabled"

Reading and writing values

\Session::set("key", "value");
\Session::get("key", "default_value"); // "default_value" if "key" is not set
\Session::contains("key"); // bool
\Session::delete("key");

Flash data

Flash data is session data meant to be read once, on the very next request, typically to show a message or refill a form after a redirect. The redirect() helper (see the Routing documentation) already uses \Session internally for this:

return redirect(url("some.route"), [
    "with_inputs" => true, // flashes the current request's input
    "messages" => ["error" => "Something went wrong"]
]);

On the next request you can read it back with:

\Session::get_message("error"); // "Something went wrong"
\Session::get_input("some_field"); // the previous request's value for "some_field"

Both get_message and get_input accept dot notation to read nested keys, and a default value as the second argument.

Flash data is cleared at the start of every redirect() call, so it survives exactly one request.

\Session also keeps track of the pages the user has visited, useful to build "go back" links. See the Navigation history documentation for how it works.