├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Facades │ └── Flash.php ├── FlashHandler.php ├── FlashServiceProvider.php ├── LaravelSessionStore.php └── SessionStore.php ├── tests └── FlashTest.php └── views └── message.blade.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Shea Lewis 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Caffeinated Flash Messages 2 | ========================== 3 | [![Source](http://img.shields.io/badge/source-caffeinated/flash-blue.svg?style=flat-square)](https://github.com/caffeinated/flash) 4 | [![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://tldrlegal.com/license/mit-license) 5 | 6 | Laravel 5 flash messages, based off the Laracasts video tutorial on the same topic: [Flexible Flash Messages](https://laracasts.com/lessons/flexible-flash-messages). Originally developed for [FusionCMS](https://github.com/fusioncms/fusioncms), an open source content management system. 7 | 8 | Quick Installation 9 | ------------------ 10 | Begin by installing the package through Composer. 11 | 12 | ``` 13 | composer require caffeinated/flash 14 | ``` 15 | 16 | Once this operation is complete, simply add both the service provider and facade classes to your project's `config/app.php` file: 17 | 18 | #### Service Provider 19 | ```php 20 | Caffeinated\Flash\FlashServiceProvider::class, 21 | ``` 22 | 23 | #### Facade 24 | ```php 25 | 'Flash' => Caffeinated\Flash\Facades\Flash::class, 26 | ``` 27 | 28 | And that's it! With your coffee in reach, start flashing out messages! 29 | 30 | Usage 31 | ----- 32 | Usage is simple. Before redirecting to another page, simply call on `Flash` to set your desired flash message. There are a number of methods to assign different levels of priority (info, success, warning, and error). 33 | 34 | #### Success 35 | 36 | ```php 37 | Flash::success('This is a success message.'); 38 | ``` 39 | 40 | #### Info 41 | 42 | ```php 43 | Flash::info('This is an info message.'); 44 | ``` 45 | 46 | #### Warning 47 | 48 | ```php 49 | Flash::warning('This is a warning message.'); 50 | ``` 51 | 52 | #### Error 53 | 54 | ```php 55 | Flash::error('This is an error message.'); 56 | ``` 57 | 58 | ### Rendering 59 | To render your flash messages in your view, simply include the bundled view partial in your master layout: 60 | 61 | ```php 62 | @include('flash::message') 63 | ``` 64 | 65 | Note that the bundled view partial is geared for Bootstrap out of the box. 66 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "caffeinated/flash", 3 | "description": "Flash Messages for Laravel", 4 | "keywords": ["flash", "laravel", "caffeinated"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Shea Lewis", 9 | "email": "shea.lewis89@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.2|^8.0", 14 | "illuminate/support": "^6.0|^7.0|^8.0" 15 | }, 16 | "require-dev": { 17 | "mockery/mockery": "^1.0" 18 | }, 19 | "autoload": { 20 | "psr-4": { 21 | "Caffeinated\\Flash\\": "src" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Facades/Flash.php: -------------------------------------------------------------------------------- 1 | session = $session; 19 | } 20 | 21 | /** 22 | * Create an info flash message. 23 | * 24 | * @param string $message 25 | */ 26 | public function info($message) 27 | { 28 | $this->message($message, 'info'); 29 | } 30 | 31 | /** 32 | * Create a success flash message. 33 | * 34 | * @param string $message 35 | */ 36 | public function success($message) 37 | { 38 | $this->message($message, 'success'); 39 | } 40 | 41 | /** 42 | * Create a warning flash message. 43 | * 44 | * @param string $message 45 | */ 46 | public function warning($message) 47 | { 48 | $this->message($message, 'warning'); 49 | } 50 | 51 | /** 52 | * Create an error flash message. 53 | * 54 | * @param string $message 55 | */ 56 | public function error($message) 57 | { 58 | $this->message($message, 'danger'); 59 | } 60 | 61 | /** 62 | * Create a flash message. 63 | * 64 | * @param string $message 65 | * @param string $level 66 | */ 67 | public function message($message, $level = 'info') 68 | { 69 | $this->session->flash('caffeinated.flash.message', $message); 70 | $this->session->flash('caffeinated.flash.level', $level); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/FlashServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__.'/../views', 'flash'); 23 | 24 | $this->publishes([ 25 | __DIR__.'/../views' => base_path('resources/views/vendor/flash'), 26 | ]); 27 | } 28 | 29 | /** 30 | * Register the service provider 31 | * 32 | * @return void 33 | */ 34 | public function register() 35 | { 36 | $this->app->bind( 37 | 'Caffeinated\Flash\SessionStore', 38 | 'Caffeinated\Flash\LaravelSessionStore' 39 | ); 40 | 41 | $this->app->singleton('flash', function() { 42 | return $this->app->make('Caffeinated\Flash\FlashHandler'); 43 | }); 44 | } 45 | 46 | /** 47 | * Get the services provided by the provider. 48 | * 49 | * @return array 50 | */ 51 | public function provides() 52 | { 53 | return ['flash']; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/LaravelSessionStore.php: -------------------------------------------------------------------------------- 1 | session = $session; 19 | } 20 | 21 | /** 22 | * Flash a message to the session. 23 | * 24 | * @param string $name 25 | * @param mixed $data 26 | */ 27 | public function flash($name, $data) 28 | { 29 | $this->session->flash($name, $data); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/SessionStore.php: -------------------------------------------------------------------------------- 1 | session = Mockery::mock('Caffeinated\Flash\SessionStore'); 14 | $this->flash = new FlashHandler($this->session); 15 | } 16 | 17 | /** @test */ 18 | public function it_displays_a_default_flash_notification() 19 | { 20 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Live long and prosper.'); 21 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Notice'); 22 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'info'); 23 | 24 | $this->flash->message('Live long and prosper.'); 25 | } 26 | 27 | /** @test */ 28 | public function it_displays_an_info_flash_notification() 29 | { 30 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Logic is the beginning of wisdom, not the end.'); 31 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Notice'); 32 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'info'); 33 | 34 | $this->flash->info('Logic is the beginning of wisdom, not the end.'); 35 | } 36 | 37 | /** @test */ 38 | public function it_displays_a_warning_flash_notification() 39 | { 40 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Highly illogical.'); 41 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Notice'); 42 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'warning'); 43 | 44 | $this->flash->warning('Highly illogical.'); 45 | } 46 | 47 | /** @test */ 48 | public function it_displays_an_error_flash_notification() 49 | { 50 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Our ship is being hit, shields at 60%.'); 51 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Notice'); 52 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'danger'); 53 | 54 | $this->flash->error('Our ship is being hit, shields at 60%.'); 55 | } 56 | 57 | /** @test */ 58 | public function it_displays_a_success_flash_notification() 59 | { 60 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Shields at 100%.'); 61 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Notice'); 62 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'success'); 63 | 64 | $this->flash->success('Shields at 100%.'); 65 | } 66 | 67 | /** @test */ 68 | public function it_displays_a_custom_titled_flash_notification() 69 | { 70 | $this->session->shouldReceive('flash')->with('caffeinated.flash.message', 'Make it so.'); 71 | $this->session->shouldReceive('flash')->with('caffeinated.flash.title', 'Mr. Worf'); 72 | $this->session->shouldReceive('flash')->with('caffeinated.flash.level', 'success'); 73 | 74 | $this->flash->success('Make it so.'); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /views/message.blade.php: -------------------------------------------------------------------------------- 1 | @if (Session::has('caffeinated.flash.message')) 2 |
3 | 4 | 5 | {{ Session::get('caffeinated.flash.message') }} 6 |
7 | @endif 8 | --------------------------------------------------------------------------------