You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/1-essentials/01-routing.md
+59Lines changed: 59 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -690,6 +690,65 @@ final readonly class RestrictedRoute implements Route
690
690
691
691
This attribute can be used in place of the usual route attributes, on controller action methods.
692
692
693
+
## Session management
694
+
695
+
Sessions in Tempest are managed by the {b`Tempest\Http\Session\Session`} class. You can inject it anywhere you need it. As soon as the `Session` is injected, it will be started behind the scenes.
696
+
697
+
```php
698
+
use Tempest\Http\Session\Session;
699
+
700
+
final readonly class TodoController
701
+
{
702
+
public function __construct(
703
+
private Session $session,
704
+
) {}
705
+
706
+
#[Post('/select/{todo}']
707
+
public function select(Todo $todo): View
708
+
{
709
+
if ($this->session->get('selected_todo') === $todo->id) {
710
+
$this->session->remove('selected_todo');
711
+
} else {
712
+
$this->session->set('selected_todo', $todo->id);
713
+
}
714
+
715
+
return $this->list();
716
+
}
717
+
}
718
+
```
719
+
720
+
### Flashing values
721
+
722
+
When you need to "flash" something to the user — in other words: show something once and clear if after refresh — you can use the `flash()` method on the session:
723
+
724
+
```php
725
+
public function store(Todo $todo): Redirect
726
+
{
727
+
$this->session->flash('message', 'Save was successful');
728
+
729
+
return new Redirect('/');
730
+
}
731
+
```
732
+
733
+
### Session configuration
734
+
735
+
Currently, there's only a built-in file session driver. More drivers are to be added in the future. By default, the session will stay valid for 10 hours, which you can overwrite by creating a `session.config.php` file:
736
+
737
+
```php app/Config/session.config.php
738
+
<?php
739
+
use Tempest\Http\Session\Config\FileSessionConfig;
740
+
use Tempest\DateTime\Duration;
741
+
742
+
return new FileSessionConfig(
743
+
path: 'sessions', // The path is relative to the project's cache folder
744
+
expiration: Duration::days(30),
745
+
);
746
+
```
747
+
748
+
### Session cleaning
749
+
750
+
Outdated sessions should occasionally be cleaned up. Tempest comes with a built-in command to do so: `tempest session:clean`. This command makes use of the [scheduler](/2.x/features/scheduling). If you have scheduling enabled, it will automatically run behind the scenes.
751
+
693
752
## Deferring tasks
694
753
695
754
It is sometimes needed, during requests, to perform tasks that would take a few seconds to complete. This could be sending an email, or keeping track of a page visit.
0 commit comments