├── src ├── helpers.php ├── Message.php └── Flash.php ├── LICENSE.md ├── composer.json ├── CHANGELOG.md └── README.md /src/helpers.php: -------------------------------------------------------------------------------- 1 | flash($message); 21 | 22 | return $flash; 23 | } 24 | -------------------------------------------------------------------------------- /src/Message.php: -------------------------------------------------------------------------------- 1 | message = $message; 20 | 21 | $this->class = $class; 22 | 23 | $this->level = $level; 24 | } 25 | 26 | public function toArray(): array 27 | { 28 | return [ 29 | 'message' => $this->message, 30 | 'class' => $this->class, 31 | 'level' => $this->level, 32 | ]; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-flash", 3 | "description": "A lightweight package to flash messages", 4 | "keywords": [ 5 | "spatie", 6 | "laravel-flash" 7 | ], 8 | "homepage": "https://github.com/spatie/laravel-flash", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Freek Van der Herten", 13 | "email": "freek@spatie.be", 14 | "homepage": "https://spatie.be", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.4|^8.0", 20 | "illuminate/session": "^8.0|^9.0|^10.0|^11.0|^12.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^9.4|^10.5|^11.5.3", 24 | "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0|^10.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "Spatie\\Flash\\": "src" 29 | }, 30 | "files": [ 31 | "src/helpers.php" 32 | ] 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "Spatie\\Flash\\Tests\\": "tests" 37 | } 38 | }, 39 | "scripts": { 40 | "test": "vendor/bin/phpunit", 41 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 42 | }, 43 | "config": { 44 | "sort-packages": true 45 | }, 46 | "minimum-stability": "dev", 47 | "prefer-stable": true 48 | } 49 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-flash` will be documented in this file 4 | 5 | ## 1.10.1 - 2025-02-21 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-flash/pull/32 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-flash/compare/1.10.0...1.10.1 12 | 13 | ## 1.10.0 - 2024-02-27 14 | 15 | ### What's Changed 16 | 17 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-flash/pull/30 18 | 19 | **Full Changelog**: https://github.com/spatie/laravel-flash/compare/1.9.1...1.10.0 20 | 21 | ## 1.9.1 - 2023-01-23 22 | 23 | - support L10 24 | 25 | ## 1.9.0 - 2022-01-12 26 | 27 | - add support for Laravel 9 28 | 29 | ## 1.8.0 - 2020-11-27 30 | 31 | - add support for PHP 8 32 | 33 | ## 1.7.0 - 2020-09-08 34 | 35 | - add support for Laravel 8 36 | 37 | ## 1.6.0 - 2020-03-18 38 | 39 | - add `level` property on `flash()` 40 | 41 | ## 1.5.0 - 2020-03-01 42 | 43 | - add support for Laravel 7 44 | 45 | ## 1.4.0 - 2019-12-08 46 | 47 | - drop support for PHP 7.3 48 | 49 | ## 1.3.0 - 2019-09-04 50 | 51 | - add support for Laravel 6 52 | 53 | ## 1.2.1 - 2019-03-15 54 | 55 | - fix circular reference 56 | 57 | ## 1.2.0 - 2019-03-14 58 | 59 | - the second parameter of `flash` will now lookup a preregistered macro 60 | 61 | ## 1.1.0 - 2019-03-14 62 | 63 | - add `levels` method 64 | 65 | ## 1.0.0 - 2019-03-14 66 | 67 | - initial release 68 | -------------------------------------------------------------------------------- /src/Flash.php: -------------------------------------------------------------------------------- 1 | session = $session; 18 | } 19 | 20 | public function __get(string $name) 21 | { 22 | return $this->getMessage()->$name ?? null; 23 | } 24 | 25 | public function getMessage(): ?Message 26 | { 27 | $flashedMessageProperties = $this->session->get('laravel_flash_message'); 28 | 29 | if (! $flashedMessageProperties) { 30 | return null; 31 | } 32 | 33 | return new Message( 34 | $flashedMessageProperties['message'], 35 | $flashedMessageProperties['class'], 36 | $flashedMessageProperties['level'] 37 | ); 38 | } 39 | 40 | public function flash(Message $message): void 41 | { 42 | if ($message->class && static::hasMacro($message->class)) { 43 | $methodName = $message->class; 44 | 45 | $this->$methodName($message->message); 46 | 47 | return; 48 | } 49 | 50 | $this->flashMessage($message); 51 | } 52 | 53 | public function flashMessage(Message $message): void 54 | { 55 | $this->session->flash('laravel_flash_message', $message->toArray()); 56 | } 57 | 58 | public static function levels(array $methodClasses): void 59 | { 60 | foreach ($methodClasses as $method => $classes) { 61 | self::macro($method, fn (string $message) => $this->flashMessage(new Message($message, $classes, $method))); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # A lightweight package to flash messages 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-flash.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-flash) 4 | ![run-tests](https://github.com/spatie/laravel-flash/workflows/run-tests/badge.svg) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-flash.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-flash) 6 | 7 | This is a lightweight package to send flash messages in Laravel apps. A flash message is a message that is carried over to the next request by storing it in the session. This package only supports one single flash message at a time. 8 | 9 | This is how it can be used: 10 | 11 | ```php 12 | class MySpecialSnowflakeController 13 | { 14 | public function store() 15 | { 16 | // … 17 | 18 | flash('My message', 'my-class'); 19 | 20 | return back(); 21 | } 22 | } 23 | ``` 24 | 25 | In your view you can do this: 26 | 27 | ```blade 28 | @if (flash()->message) 29 |
30 | {{ flash()->message }} 31 |
32 | @endif 33 | ``` 34 | 35 | ## Support us 36 | 37 | [](https://spatie.be/github-ad-click/laravel-flash) 38 | 39 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 40 | 41 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 42 | 43 | ## Installation 44 | 45 | You can install the package via composer: 46 | 47 | ```bash 48 | composer require spatie/laravel-flash 49 | ``` 50 | 51 | ## Usage 52 | 53 | Here is an example on how to flash a message. 54 | 55 | ```php 56 | class MyController 57 | { 58 | public function store() 59 | { 60 | // … 61 | 62 | flash('My message'); 63 | 64 | return back(); 65 | } 66 | } 67 | ``` 68 | 69 | In your view you can use it like this 70 | 71 | ```blade 72 | @if(flash()->message) 73 |
74 | {{ flash()->message }} 75 |
76 | @endif 77 | ``` 78 | 79 | ### Using a class name to style the displayed message 80 | 81 | You can add a class as the second parameter. This is typically used to style the output in your HTML. 82 | 83 | ```php 84 | class MyController 85 | { 86 | public function store() 87 | { 88 | // … 89 | 90 | flash('My message', 'my-class'); 91 | 92 | return back(); 93 | } 94 | } 95 | ``` 96 | 97 | In your view you can use the class like this: 98 | 99 | ```blade 100 | @if (flash()->message) 101 |
102 | {{ flash()->message }} 103 |
104 | @endif 105 | ``` 106 | 107 | You can also set an array of classes. These will be output by `flash()->class` by imploding the array with a space-delimiter. 108 | 109 | ```php 110 | flash('My message', ['my-class', 'another-class']); // flash()->class output is: 'my-class another-class' 111 | ``` 112 | 113 | ### Adding your own methods 114 | 115 | If you don't want to specify a class each time you flash a message you can add a method name to `flash`. 116 | 117 | The easiest way is by passing an array to the `levels` method. The key is the method name that should be added to `flash()`. The value is the class that will automatically be used when rendering the message. 118 | 119 | ```php 120 | // this would probably go in a service provider 121 | 122 | \Spatie\Flash\Flash::levels([ 123 | 'success' => 'alert-success', 124 | 'warning' => 'alert-warning', 125 | 'error' => 'alert-error', 126 | ]); 127 | ``` 128 | 129 | The above example will make these methods available on `flash`: 130 | 131 | ```php 132 | flash()->success('Hurray'); 133 | flash()->warning('Mayybeee'); 134 | flash()->error('Oh Oh'); 135 | ``` 136 | 137 | The most likely scenario is that you want to consume the flash message in a view. You can use the `message`, `class` and `level` properties on the view. 138 | 139 | ```blade 140 | @if (flash()->message) 141 |
142 | {{ flash()->message }} 143 |
144 | 145 | @if(flash()->level === 'error') 146 | This was an error. 147 | @endif 148 | @endif 149 | ``` 150 | 151 | Additionally, when you've added your own method, you can also pass that method name as a second parameter to `flash` itself: 152 | 153 | ```php 154 | flash('Hurray', 'success'); // `flash()->class` will output 'alert-success' 155 | ``` 156 | 157 | You can also add a method to `flash` by using `macro`. 158 | 159 | Here's an example: 160 | 161 | ```php 162 | // this would probably go in a service provider 163 | 164 | use Spatie\Flash\Message; 165 | 166 | \Spatie\Flash\Flash::macro('warning', function (string $message) { 167 | return $this->flashMessage(new Message($message, 'alert alert-warning')); 168 | }); 169 | ``` 170 | 171 | You can now use a `warning` method on `flash`: 172 | 173 | ```php 174 | flash()->warning('Look above you!'); 175 | ``` 176 | 177 | You can pass the desired `level` as the third argument to `Message`. 178 | 179 | ```php 180 | // in a service provider 181 | use Spatie\Flash\Message; 182 | 183 | \Spatie\Flash\Flash::macro('warning', function (string $message) { 184 | return $this->flashMessage(new Message($message, 'alert alert-warning', 'my-level')); 185 | }); 186 | 187 | // in the next request, after having flashed a message 188 | flash()->level; // returns `my-level` 189 | ``` 190 | 191 | ## Alternatives 192 | 193 | This package is intended to be lightweight. If you need things like multiple messages, support for Bootstrap, overlays, ... take a look at [this excellent flash package](https://github.com/laracasts/flash) by [Jeffrey Way](https://github.com/JeffreyWay) or [Laraflash](https://github.com/coderello/laraflash) by [Ilya Sakovich](https://github.com/hivokas). 194 | 195 | ## Testing 196 | 197 | ``` bash 198 | composer test 199 | ``` 200 | 201 | ## Changelog 202 | 203 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 204 | 205 | ## Contributing 206 | 207 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 208 | 209 | ### Security 210 | 211 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 212 | 213 | ## Postcardware 214 | 215 | You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. 216 | 217 | Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. 218 | 219 | We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). 220 | 221 | ## Credits 222 | 223 | - [Freek Van der Herten](https://github.com/freekmurze) 224 | - [All Contributors](../../contributors) 225 | 226 | ## License 227 | 228 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 229 | --------------------------------------------------------------------------------