|
1 | 1 | # Session Flash |
2 | 2 |
|
3 | | -Session flash allows you to store data in the session for a single request. This is useful for scenarios like displaying a message after a form submission. Leaf provides a straightforward way to work with flash messages. |
| 3 | +Session flash allows you to store data in the session for a single request. This is useful for scenarios like displaying a message after a form submission or passing data from one request to another. |
4 | 4 |
|
5 | 5 | ## Adding a new flash |
6 | 6 |
|
7 | | -You can set a new flash item using the `set()` method. It method accepts two arguments: |
| 7 | +You can add a new flash message to a response using the `withFlash()` method. This method accepts two arguments: |
8 | 8 |
|
9 | | -- The item to flash |
10 | | -- The key to save it under. The key is optional and defaults to `message`. |
| 9 | +- The name of the flash message |
| 10 | +- The value of the flash message |
| 11 | + |
| 12 | +```php:no-line-numbers |
| 13 | +response()->withFlash('message', 'something'); |
| 14 | +``` |
| 15 | + |
| 16 | +You can chain the `withFlash()` method with your main response methods to return a response with a flash message. |
11 | 17 |
|
12 | 18 | ```php |
13 | | -flash()->set('This is my message'); |
14 | | -flash()->set('This is my message', 'info'); |
| 19 | +response() |
| 20 | + ->withFlash('message', 'something') |
| 21 | + ->json('...'); |
15 | 22 | ``` |
16 | 23 |
|
17 | 24 | You are not limited to strings. You can flash different types of data: |
18 | 25 |
|
19 | 26 | ```php |
20 | | -flash()->set($userObject); |
21 | | -flash()->set($userArray); |
22 | | -flash()->set($userString); |
23 | | -flash()->set($userInt); |
| 27 | +response()->withFlash('object', $userObject)->json('...'); |
| 28 | +response()->withFlash('array', $userArray)->json('...'); |
| 29 | +response()->withFlash('string', $userString)->json('...'); |
| 30 | +response()->withFlash('int', $userInt)->json('...'); |
24 | 31 | ``` |
25 | 32 |
|
26 | 33 | ## Display a flash item |
27 | 34 |
|
28 | 35 | To display a flash item, you can use the `display()` method. This method accepts the key of the item to get. If the key is not provided, it defaults to `message`. |
29 | 36 |
|
30 | | -```php |
31 | | -echo flash()->display(); |
| 37 | +```php:no-line-numbers |
| 38 | +$message = flash()->display(); |
32 | 39 | ``` |
33 | 40 |
|
34 | | -If you set a flash item with a key, you can pass the key to the `display()` method to get the item. |
| 41 | +If you set a flash item with a different key, you can pass the key to the `display()` method to get the item. |
35 | 42 |
|
36 | 43 | ```php |
37 | | -flash()->set('This is my message', 'info'); |
38 | | -... |
39 | | - |
40 | | -echo flash()->display('info'); |
| 44 | +$message = flash()->display('info'); |
| 45 | +$object = flash()->display('object'); |
| 46 | +$array = flash()->display('array'); |
41 | 47 | ``` |
42 | 48 |
|
43 | 49 | The item will be removed from the session after it has been displayed. |
44 | 50 |
|
45 | 51 | ## Manually removing a flash item |
46 | 52 |
|
47 | | -You may choose to remove a flash item manually using the `remove()` method. This method accepts the key of the item to remove. |
| 53 | +You may choose to remove a flash item manually without displaying it first. You can do this by calling the `remove()` method with the key of the item to remove. |
48 | 54 |
|
49 | | -```php |
| 55 | +```php:no-line-numbers |
50 | 56 | flash()->remove('info'); |
51 | 57 | ``` |
0 commit comments