├── views └── flashes.blade.php ├── CHANGELOG.md ├── start.php ├── config └── flasher.php ├── libraries ├── message.php └── flasher.php └── README.md /views/flashes.blade.php: -------------------------------------------------------------------------------- 1 | {{ Flasher::showall() }} 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## v1.0.0-beta - 27-Mar-2013 4 | 5 | - initial public release 6 | -------------------------------------------------------------------------------- /start.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | 11 | 12 | Autoloader::namespaces(array( 13 | 'Flasher' => Bundle::path('flasher') . 'libraries' 14 | )); 15 | -------------------------------------------------------------------------------- /config/flasher.php: -------------------------------------------------------------------------------- 1 | format(); 10 | * 11 | * or when using the magic __toString() method: 12 | * 13 | * echo $msg; 14 | * 15 | */ 16 | 17 | 'format' => '

:message

', 18 | 19 | ); 20 | -------------------------------------------------------------------------------- /libraries/message.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | 11 | 12 | use Laravel\Config; 13 | 14 | 15 | class Message { 16 | 17 | /** 18 | * The string message. 19 | * @var string 20 | */ 21 | public $message; 22 | 23 | /** 24 | * The type of message (e.g. "error", "info", "success", etc.). 25 | * Can be anything, really. 26 | * @var string 27 | */ 28 | public $type; 29 | 30 | 31 | /** 32 | * Create a new Flasher message object. 33 | * 34 | * @param string $message 35 | * @param string $type 36 | */ 37 | public function __construct( $message, $type=null ) 38 | { 39 | $this->message = $message; 40 | $this->type = $type; 41 | } 42 | 43 | 44 | /** 45 | * Return a formatted message string. 46 | * 47 | * @param string $format 48 | * @return string 49 | */ 50 | public function format( $format = null ) 51 | { 52 | 53 | if ( $format === null ) { 54 | $format = Config::get( 'flasher.format', Config::get( 'flasher::flasher.format', ':message' ) ); 55 | } 56 | 57 | return str_replace( 58 | array( ':type', ':message' ), 59 | array( $this->type, $this->message ), 60 | $format 61 | ); 62 | 63 | } 64 | 65 | 66 | /** 67 | * Magic method, casts object to string by formatting it. 68 | * 69 | * @return string 70 | */ 71 | public function __toString() 72 | { 73 | return $this->format(); 74 | } 75 | 76 | 77 | } 78 | -------------------------------------------------------------------------------- /libraries/flasher.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | 11 | 12 | use Laravel\Session; 13 | 14 | 15 | class Flasher { 16 | 17 | 18 | /** 19 | * The session key when Flasher messages are stored. 20 | */ 21 | private static $sess_key = 'flasher'; 22 | 23 | 24 | /** 25 | * Load the array of Flasher messages from the session, or return an empty. 26 | * 27 | * @return array 28 | */ 29 | private static function load() 30 | { 31 | if ( $data = Session::get( static::$sess_key ) ) { 32 | return unserialize($data); 33 | } 34 | return array(); 35 | } 36 | 37 | 38 | /** 39 | * Filter an array of Flasher messages by type. 40 | * 41 | * @param array $array 42 | * @param string $type 43 | * @return array 44 | */ 45 | private static function filter( array $array, $type = null ) 46 | { 47 | if ( $type ) 48 | { 49 | $array = array_filter($array, function($msg) use ($type) { 50 | return $msg->type == $type; 51 | }); 52 | } 53 | return $array; 54 | } 55 | 56 | 57 | /** 58 | * Save the Flasher message array to the session. 59 | * 60 | * @param array $array 61 | * @return void 62 | */ 63 | private static function flash( $array ) 64 | { 65 | Session::flash( static::$sess_key, serialize( $array ) ); 66 | } 67 | 68 | 69 | /** 70 | * Add a message to the Flasher message array 71 | * 72 | * @param string $message 73 | * @param string $type 74 | */ 75 | public static function add($message, $type = null ) 76 | { 77 | 78 | $flasher = static::load(); 79 | 80 | $message = new Message($message, $type); 81 | 82 | $flasher[] = $message; 83 | 84 | static::flash( $flasher ); 85 | 86 | } 87 | 88 | 89 | /** 90 | * Return all the Flasher messages, optionally filtering on type, 91 | * formatted using the default format, and concatenated. 92 | * 93 | * @param string $type 94 | * @return string 95 | */ 96 | public static function showall( $type = null ) 97 | { 98 | return join( PHP_EOL, static::all($type) ); 99 | } 100 | 101 | 102 | /** 103 | * Return all the Flasher messages, optionally filtering on type. 104 | * 105 | * @param string $type 106 | * @return array 107 | */ 108 | public static function all( $type = null ) 109 | { 110 | return static::filter( static::load(), $type ); 111 | } 112 | 113 | 114 | /** 115 | * Return the first Flasher message, optionally filtering on type. 116 | * 117 | * @param string $type 118 | * @return Flasher\Message 119 | */ 120 | public static function first( $type = null ) 121 | { 122 | $all = static::all($type); 123 | return array_shift( $all ); 124 | } 125 | 126 | 127 | /** 128 | * Return the last Flasher message, optionally filtering on type. 129 | * 130 | * @param string $type 131 | * @return Flasher\Message 132 | */ 133 | public static function last( $type = null ) 134 | { 135 | $all = static::all($type); 136 | return array_pop( $all ); 137 | } 138 | 139 | 140 | /** 141 | * Return whether or not there are any Flasher messages, or whether there 142 | * are any messages of a given type. 143 | * 144 | * @param string $type 145 | * @return boolean 146 | */ 147 | public static function has( $type = null ) 148 | { 149 | $all = static::all($type); 150 | return count( $all ) > 0; 151 | } 152 | 153 | /** 154 | * Magic method to dynamically store a Flasher message. 155 | * 156 | * @param string $method 157 | * @param array $parameters First element is the message 158 | */ 159 | public static function __callStatic( $method, $parameters ) 160 | { 161 | static::add( $parameters[0], $method ); 162 | } 163 | 164 | 165 | } 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-Flasher 2 | 3 | A simple flash-message handler for the [Laravel](http://laravel.com) framework. 4 | 5 | 6 | ## Installation 7 | 8 | Install the bundle with artisan: 9 | 10 | ``` 11 | php artisan bundle::install flasher 12 | ``` 13 | 14 | Update your `application/bundles.php` file: 15 | 16 | ```php 17 | 'flasher' => array( 'auto' => true ), 18 | ``` 19 | 20 | And add an alias to `application/config/application.php` for ease-of-use: 21 | 22 | ```php 23 | ... 24 | 'aliases' => array( 25 | ... 26 | 'Flasher' => 'Flasher\\Flasher', 27 | ... 28 | ), 29 | ... 30 | ``` 31 | 32 | 33 | ## Sample Usage 34 | 35 | In your controllers, flash the message before your redirect. 36 | 37 | ```php 38 | public function post_login() 39 | { 40 | 41 | $credentials = Input::all(); 42 | 43 | if ( Auth::attempt($credentials) ) { 44 | 45 | Flasher::success('Welcome back!'); 46 | return Redirect::to('home'); 47 | 48 | } 49 | 50 | Flasher::error('Login failed'); 51 | 52 | return Redirect::back(); 53 | 54 | } 55 | ``` 56 | 57 | In your views, all you need to see the flashed messages is: 58 | 59 | ```php 60 | @render('flasher::flashes') 61 | ``` 62 | 63 | Or render them directly with: 64 | 65 | ```php 66 | {{ Flasher::showall() }} 67 | ``` 68 | 69 | 70 | 71 | ## Customizing 72 | 73 | The output format of the flash is stored in a configuration file. Simply copy the `bundles/flasher/config/flasher.php` to `application/config/flasher.php` and adjust it as required. 74 | 75 | Alternatively, you can copy the view from `bundles/flasher/views/flashes.php` into your application's views and `@render()` that instead. See [Working with Messages] for details. 76 | 77 | 78 | 79 | ## API 80 | 81 | ### Adding Messages 82 | 83 | To add a flash message of a certain type: 84 | 85 | ```php 86 | Flasher::add( $message, $type = null ) 87 | ``` 88 | 89 | This adds a message to the flash array. A more common way to do this would be to use a magic method: 90 | 91 | ``` 92 | Flasher::error('Something has gone wrong!'); 93 | Flasher::success('You did it!'); 94 | Flasher::info('File size is '.$size); 95 | ``` 96 | 97 | Any value can be used for `$type` (or for a magic method), except for the retrieval methods below. 98 | 99 | 100 | ### Retrieve Messages 101 | 102 | To retrieve all messages (returns an array of `Flasher\Message` objects): 103 | 104 | ```php 105 | Flasher::all() 106 | ``` 107 | 108 | Retrieve all messages of type "error": 109 | 110 | ```php 111 | Flasher::all('error') 112 | ``` 113 | 114 | Retrieve first (or last) message (returns a single `Flasher\Message` object): 115 | 116 | ```php 117 | Flasher::first() 118 | Flasher::last() 119 | ``` 120 | 121 | Retrieve first (or last) message of a type: 122 | 123 | ```php 124 | Flasher::first('error') 125 | Flasher::last('success') 126 | ``` 127 | 128 | Check if there are any error messages (returns boolean): 129 | 130 | ```php 131 | if ( Flasher::has('error') ) { 132 | ... 133 | } 134 | ``` 135 | 136 | Check if there are __any__ messages: 137 | 138 | ```php 139 | if ( Flasher::has() ) { 140 | ... 141 | } 142 | ``` 143 | 144 | 145 | ### Displaying Messages 146 | 147 | The `Flasher\Message` objects have `type` and `message` attributes at your disposal. You can use these directly in views, for example. 148 | 149 | ```php 150 | @foreach( Flasher::get() as $msg ) 151 |
152 | {{ $msg->message }} 153 |
154 | @endforeach 155 | ``` 156 | 157 | `Flasher\Message` objects also have a `format()` method which takes a string, similar to Laravel's `Messages` class. You can pass a string with embedded `:type` and `:message` strings, and they will be replaced with the appropriate values: 158 | 159 | ```php 160 | echo $msg->format('

:message

'); 161 | ``` 162 | 163 | Calling the `format()` method without an argument will use whatever is defined by the `format` key in `application/config/flasher.php`, falling back to the key defined in `bundles/flasher/config/flasher.php` (which is the same as the above example). 164 | 165 | Finally, the `Flasher\Message` object has a magic `__toString()` which just calls `format()` with no arguments (i.e. use the configuration format). 166 | 167 | We've added a quick way to display all the formatted messages (optionally filtering on type): 168 | 169 | ```php 170 | echo Flasher::showall($type); 171 | ``` 172 | 173 | This is pretty much equivalent to: 174 | 175 | ```php 176 | foreach( Flasher::all($type) as $message ) { 177 | echo $message->format(); 178 | } 179 | ``` 180 | 181 | 182 | ## Bugs and Suggestions 183 | 184 | Use the [Github Issues](https://github.com/cviebrock/laravel-flasher/issues) sysyem to report bugs or suggest improvements. Even better, fork this repository and submit a pull request. 185 | 186 | 187 | 188 | ## Todos 189 | 190 | - Convert to Laravel 4 composer package 191 | --------------------------------------------------------------------------------