├── src ├── config │ └── toastr.php ├── views │ └── toastr.blade.php ├── Facades │ └── Toastr.php ├── ToastrServiceProvider.php └── Toastr.php ├── composer.json └── README.md /src/config/toastr.php: -------------------------------------------------------------------------------- 1 | [], 5 | ]; 6 | -------------------------------------------------------------------------------- /src/views/toastr.blade.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/Facades/Toastr.php: -------------------------------------------------------------------------------- 1 | Yuansir\Toastr\Facades\Toastr::class,` to **aliases** in *config/app.php* 12 | - Run `php artisan vendor:publish` 13 | 14 | 15 | 16 | ## Usage 17 | 18 | Just add this code to your blade template file: 19 | 20 | ``` 21 | {!! Toastr::render() !!} 22 | ``` 23 | 24 | Use these methods in controllers: 25 | 26 | - `Toastr::warning($message, $title = null, $options = [])`  27 | - `Toastr::error($message, $title = null, $options = [])`  28 | - `Toastr::info($message, $title = null, $options = [])` 29 | - `Toastr::success($message, $title = null, $options = [])` 30 | - `Toastr::clear() ` 31 | 32 | 33 | 34 | ## Config 35 | 36 | set the toaster options in **config/toastr.php** , available options => [toastr.js demo](http://codeseven.github.io/toastr/demo.html) 37 | 38 | -------------------------------------------------------------------------------- /src/ToastrServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__.'/views', 'Toastr'); 17 | 18 | $this->publishes([ 19 | __DIR__.'/views' => base_path('resources/views/vendor/Toastr'), 20 | __DIR__.'/config/toastr.php' => config_path('toastr.php'), 21 | ]); 22 | } 23 | 24 | /** 25 | * Register the application services. 26 | * 27 | * @return void 28 | */ 29 | public function register() 30 | { 31 | $this->app->singleton('toastr', function ($app) { 32 | return new Toastr($app['session'], $app['config']); 33 | }); 34 | } 35 | 36 | /** 37 | * Get the services provided by the provider. 38 | * 39 | * @return array 40 | */ 41 | public function provides() 42 | { 43 | return ['toastr']; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/Toastr.php: -------------------------------------------------------------------------------- 1 | session = $session; 34 | $this->config = $config; 35 | } 36 | 37 | public function render() 38 | { 39 | $notifications = $this->session->get('toastr:notifications'); 40 | 41 | if (!$notifications) { 42 | return ''; 43 | } 44 | 45 | foreach ($notifications as $notification) { 46 | $config = $this->config->get('toastr.options'); 47 | $javascript = ''; 48 | $options = []; 49 | if ($config) { 50 | $options = array_merge($config, $notification['options']); 51 | } 52 | 53 | if ($options) { 54 | $javascript = 'toastr.options = '.json_encode($options).';'; 55 | } 56 | 57 | $message = str_replace("'", "\\'", $notification['message']); 58 | $title = $notification['title'] ? str_replace("'", "\\'", $notification['title']) : null; 59 | $javascript .= " toastr.{$notification['type']}('$message','$title');"; 60 | } 61 | 62 | return view('Toastr::toastr', compact('javascript')); 63 | } 64 | 65 | /** 66 | * Add notification. 67 | * 68 | * @param $type 69 | * @param $message 70 | * @param null $title 71 | * @param array $options 72 | * 73 | * @return bool 74 | */ 75 | public function add($type, $message, $title = null, $options = []) 76 | { 77 | $types = ['info', 'warning', 'success', 'error']; 78 | if (!in_array($type, $types)) { 79 | return false; 80 | } 81 | 82 | $this->notifications[] = [ 83 | 'type' => $type, 84 | 'title' => $title, 85 | 'message' => $message, 86 | 'options' => $options, 87 | ]; 88 | $this->session->flash('toastr:notifications', $this->notifications); 89 | } 90 | 91 | /** 92 | * Add info notification. 93 | * 94 | * @param $message 95 | * @param null $title 96 | * @param array $options 97 | */ 98 | public function info($message, $title = null, $options = []) 99 | { 100 | $this->add('info', $message, $title, $options); 101 | } 102 | 103 | /** 104 | * Add warning notification. 105 | * 106 | * @param $message 107 | * @param null $title 108 | * @param array $options 109 | */ 110 | public function warning($message, $title = null, $options = []) 111 | { 112 | $this->add('warning', $message, $title, $options); 113 | } 114 | 115 | /** 116 | * Add success notification. 117 | * 118 | * @param $message 119 | * @param null $title 120 | * @param array $options 121 | */ 122 | public function success($message, $title = null, $options = []) 123 | { 124 | $this->add('success', $message, $title, $options); 125 | } 126 | 127 | /** 128 | * Add error notification. 129 | * 130 | * @param $message 131 | * @param null $title 132 | * @param array $options 133 | */ 134 | public function error($message, $title = null, $options = []) 135 | { 136 | $this->add('error', $message, $title, $options); 137 | } 138 | 139 | /** 140 | * Clear notifications. 141 | */ 142 | public function clear() 143 | { 144 | $this->notifications = []; 145 | } 146 | } 147 | --------------------------------------------------------------------------------