├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── Facades └── Notify.php ├── Notify.php ├── NotifyServiceProvider.php └── config └── config.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 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 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Notify (Laravel 5 Package) 2 | 3 | Notifications for Laravel 5. This is a simple package that extends Illuminate MessageBag. 4 | It provides a simple interface for displaying notifications. 5 | 6 | Please note, to show notifications on redirect this uses **session flashing**, therefore the messages are only stored for 1 page redirect, not 7 | indefinitely. 8 | 9 | ## Installation 10 | Require this package with composer using the following command: 11 | 12 | composer require coderjp/notify 13 | 14 | Add the service provider to the `providers` array in `config/app.php` 15 | 16 | 'Coderjp\Notify\NotifyServiceProvider', 17 | 18 | Add the facade to the `aliases` array in `config/app.php` 19 | 20 | 'Notify' => 'Coderjp\Notify\Facades\Notify', 21 | 22 | Generate the config file for changing various settings. This can be found in `config/notify.php`. 23 | 24 | php artisan vendor:publish --provider=Coderjp\\Notify\\NotifyServiceProvider 25 | 26 | ## Configuration 27 | 28 | To add your own message types, add them to the `types` array in `config/notify.php`. 29 | By default the options are `success, error, info`. 30 | 31 | ## Usage 32 | 33 | ### Storing Notifications 34 | 35 | Notifications can be either be displayed on redirect or on the current page. 36 | 37 | To store a notification so it is **displayed on the next redirect**, call the type like so 38 | 39 | ```php 40 | Notify::success('The user was added!'); 41 | Notify::error('There was a problem adding the user, Please try again'); 42 | Notify::info('The user\'s password was changed'); 43 | ``` 44 | 45 | To store a notification so it is **displayed on the current page**, call the type like so 46 | 47 | ```php 48 | Notify::successNow('The user was added!'); 49 | Notify::errorNow('There was a problem adding the user, Please try again'); 50 | Notify::infoNow('The user\'s password was changed'); 51 | ``` 52 | 53 | ### Outputing Notifications 54 | 55 | To output a certain type of notification 56 | 57 | ```php 58 | if (Notify::has('success') 59 |
60 | 65 |
66 | @endif 67 | ``` 68 | 69 | To output all/any 70 | 71 | ```php 72 | if (Notify::all()) 73 |
74 | 79 |
80 | @endif 81 | ``` 82 | 83 | ## Contributions 84 | 85 | If you feel that this project should do more, please open a pull request or open an issue for future improvements / functionality. 86 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coderjp/notify", 3 | "description": "Notifications for Laravel 5", 4 | "keywords": [ 5 | "laravel", 6 | "notify", 7 | "message", 8 | "alert" 9 | ], 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Jack Powell", 14 | "homepage": "https://github.com/coderjp" 15 | } 16 | ], 17 | "minimum-stability": "dev", 18 | "require": { 19 | "php": ">=5.4.0", 20 | "illuminate/support": "~5.0", 21 | "illuminate/session": "~5.0", 22 | "illuminate/config": "~5.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "Coderjp\\Notify\\": "src" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Facades/Notify.php: -------------------------------------------------------------------------------- 1 | app = $app; 31 | 32 | parent::__construct($this->app->session->get($this->getSessionName(), [])); 33 | } 34 | 35 | /** 36 | * Gets all the message types from the config 37 | * @return mixed 38 | */ 39 | public function getTypes() 40 | { 41 | return $this->app->config->get('notify.types'); 42 | } 43 | 44 | /** 45 | * Adds the message to the MessageBag 46 | * @param $type 47 | * @param $message 48 | */ 49 | public function storeNotification($type, $message, $flash = true) 50 | { 51 | $this->add($type, $message); 52 | 53 | if ($flash) { 54 | $this->app->session->flash($this->getSessionName(), $this->messages); 55 | } 56 | } 57 | 58 | /** 59 | * Gets the session name 60 | * @return mixed 61 | */ 62 | public function getSessionName() 63 | { 64 | return $this->app->config->get('notify.session_name'); 65 | } 66 | 67 | /** 68 | * Dynamically add messages 69 | * @param $method 70 | * @param $params 71 | */ 72 | public function __call($method, $params) 73 | { 74 | $flash = true; 75 | preg_match('/([a-zA-Z]+)Now/', $method, $matches); 76 | 77 | if (count($matches) === 2) { 78 | $method = $matches[1]; 79 | $flash = false; 80 | } 81 | 82 | if (!in_array($method, $this->getTypes())) { 83 | throw new BadMethodCallException("Method [$method] does not exist"); 84 | } 85 | 86 | if (!is_array($params)) { 87 | $params = [$params]; 88 | } 89 | 90 | foreach ($params as $message) { 91 | $this->storeNotification($method, $message, $flash); 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /src/NotifyServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 22 | __DIR__.'/config/config.php' => config_path('notify.php'), 23 | ]); 24 | } 25 | 26 | /** 27 | * Register the service provider. 28 | * 29 | * @return void 30 | */ 31 | public function register() 32 | { 33 | $this->registerNotify(); 34 | $this->mergeConfig(); 35 | } 36 | 37 | /** 38 | * Register the application bindings. 39 | * 40 | * @return void 41 | */ 42 | private function registerNotify() 43 | { 44 | $this->app->bind('notify', function ($app) { 45 | return new Notify($app); 46 | }); 47 | } 48 | 49 | /** 50 | * Merges user's and notify's configs. 51 | * 52 | * @return void 53 | */ 54 | private function mergeConfig() 55 | { 56 | $this->mergeConfigFrom( 57 | __DIR__.'/config/config.php', 'notify' 58 | ); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | ['success', 'error', 'info'], 18 | /* 19 | |-------------------------------------------------------------------------- 20 | | Session Name 21 | |-------------------------------------------------------------------------- 22 | | 23 | | This is the name/key used in the Session Manager 24 | | 25 | */ 26 | 'session_name' => 'notify', 27 | ); --------------------------------------------------------------------------------