diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5826402 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f60bbe0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..12a934d --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Change Log +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + + +## [1.0.0] - 2015-03-09 10:47 GMT +- Initial release. + + + +[1.0.0]: https://github.com/Grimthorr/laravel-toast/tree/1.0.0 diff --git a/README.md b/README.md new file mode 100644 index 0000000..006c7f2 --- /dev/null +++ b/README.md @@ -0,0 +1,151 @@ +# laravel-toast +Simple toast messages for Laravel 5. + + +## Installation +1. Run `composer require grimthorr/laravel-toast` to include this in your project. +2. Add `'Grimthorr\LaravelToast\ServiceProvider'` to `providers` in `config/app.php`. + + ```php + 'providers' => array( + // ... + 'Grimthorr\LaravelToast\ServiceProvider', + ), + ``` + +3. Add `'Toast' => 'Grimthorr\LaravelToast\Facade'` to `aliases` in `config/app.php`. + + ```php + 'aliases' => array( + // ... + 'Toast' => 'Grimthorr\LaravelToast\Facade', + ), + ``` + +4. *Optional*: Run `php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="config"` to publish the config file. +5. *Optional*: Modify the published configuration file located at `config/laravel-toast.php` to your liking. +6. *Optional*: Run `php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="views"` to publish the views. +7. *Optional*: Modify the published views located at `resources/views/vendor/toast` to your liking. + + +## Configuration +Pop open `config/laravel-toast.php` to adjust package configuration. If this file doesn't exist, run `php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="config"` to create the default configuration file. + +```php +return array( + 'levels' => array( + 'info' => 'info', + 'success' => 'success', + 'error' => 'error', + 'warning' => 'warning', + 'default' => 'info' + ), +); +``` + +#### Levels +Specify the class sent to the view for each level. For example calling the `info` method would send the `info` class to the view. If you use [Bootstrap](http://getbootstrap.com/), you could set this to `alert alert-info` for ease of use in the view. + +You can create a custom method here by passing a new level name and class. For example: `'help' => 'help'` will allow you to call `Toast::help($message)`. Alternatively, you can use the `Toast::message($message, $level)` method instead. + +#### Views +This package includes a couple of views to get you started, they can be published to your resources directory using `php artisan vendor:publish --provider="Grimthorr\LaravelToast\ServiceProvider" --tag="views"` or called straight from the package by including them in a Blade template: `@include('toast::messages')`. + +```html +@if(Session::has('toasts')) + @foreach(Session::get('toasts') as $toast) +
+ + + {{ $toast['message'] }} +
+ @endforeach +@endif +``` + + +## Usage +Use the Toast facade (`Toast::`) or the helper function (`toast()->`) to access the methods in this package. You can also chain multiple messages together using method chaining: `toast()->success('done')->info('hello')`. + +#### Message +```php +Toast::message('message', 'level'); +toast()->message('message', 'level'); +toast('message'); +``` +Add a toast to the session. Using `toast('message')` will use the default level. + +#### Info +```php +Toast::info('message'); +toast()->info('message'); +``` +Add a toast with the `info` level. + +#### Success +```php +Toast::success('message'); +toast()->success('message'); +``` +Add a toast with the `success` level. + +#### Error +```php +Toast::error('message'); +toast()->error('message'); +``` +Add a toast with the `error` level. + +#### Warning +```php +Toast::warning('message'); +toast()->warning('message'); +``` +Add a toast with the `warning` level. + + +## Example +These examples are using the default configuration. + +#### Using the facade to send an error message +The following adds an error toast to the session and then redirects to `home`. +```php +// Create the message +Toast::error('oops'); + +// Return a HTTP response to initiate the new session +return Redirect::to('home'); +``` + +#### Using method chaining to create multiple toasts +The following adds an error and info toast to the session and then redirects to `home`. +```php +// Create the message +Toast::error('oops') + ->info('hello'); + +// Return a HTTP response to initiate the new session +return Redirect::to('home'); +``` + +#### Using the helper function to send a message with a custom level +The following adds a help toast to the session and then redirects to `home`. +```php +// Create the message +Toast::message('example', 'help'); + +// Return a HTTP response to initiate the new session +return Redirect::to('home'); +``` + + +## Finally + +#### Contributing +Feel free to create a fork and submit a pull request if you would like to contribute. + +#### Bug reports +Raise an issue on GitHub if you notice something broken. + +#### Credits +Based loosely on https://github.com/laracasts/flash. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..5f98d99 --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "grimthorr/laravel-toast", + "description": "Simple toast messages for Laravel 5.", + "homepage": "https://github.com/Grimthorr/laravel-toast", + "license": "MIT", + "authors": [ + { + "name": "Grimthorr", + "email": "phil@hughesbox.co.uk", + "homepage": "http://hughesbox.co.uk" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/support": "5.*", + "illuminate/config": "5.*", + "illuminate/database": "5.*" + }, + "autoload": { + "psr-4": { + "Grimthorr\\LaravelToast\\": "src/" + }, + "files": [ + "src/helpers.php" + ] + }, + "minimum-stability": "stable" +} diff --git a/config/config.php b/config/config.php new file mode 100644 index 0000000..0aefd37 --- /dev/null +++ b/config/config.php @@ -0,0 +1,20 @@ + 'help' will allow you to call Toast::help($message). + * Alternatively, you can use the Toast::message($message, $level) method instead. + */ + 'levels' => array( + 'info' => 'info', + 'success' => 'success', + 'error' => 'error', + 'warning' => 'warning', + 'default' => 'info' + ), + +); diff --git a/src/Facade.php b/src/Facade.php new file mode 100644 index 0000000..02b6f83 --- /dev/null +++ b/src/Facade.php @@ -0,0 +1,13 @@ +publishes([ + __DIR__.'/../config/config.php' => config_path('laravel-toast.php'), + ], 'config'); + + $this->publishes([ + __DIR__.'/../views' => base_path('resources/views/vendor/toast'), + ], 'views'); + + $this->loadViewsFrom(__DIR__ . '/../views', 'toast'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->singleton('toast', 'Grimthorr\LaravelToast\Toast'); + + $this->mergeConfigFrom( + __DIR__.'/../config/config.php', 'laravel-toast' + ); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array(); + } + +} diff --git a/src/Toast.php b/src/Toast.php new file mode 100644 index 0000000..87e8001 --- /dev/null +++ b/src/Toast.php @@ -0,0 +1,125 @@ +levels = config('laravel-toast.levels'); + } + + /** + * Call method to enable custom levels specified in config. + * + * @param $method + * @param $args + * @return void + */ + function __call($method, $args) + { + if (array_key_exists($method, $this->levels)) { + call_user_func_array([$this, 'message'], $args); + } else { + throw new \BadMethodCallException('Toast config file does not contain level "' . $method . '"'); + } + } + + + /** + * Create an info message. + * + * @param string $message + * @return $this + */ + public function info($message) + { + $this->message($message, $this->levels['info']); + + return $this; + } + + /** + * Create a success message. + * + * @param string $message + * @return $this + */ + public function success($message) + { + $this->message($message, $this->levels['success']); + + return $this; + } + + /** + * Create an error message. + * + * @param string $message + * @return $this + */ + public function error($message) + { + $this->message($message, $this->levels['error']); + + return $this; + } + + /** + * Create a warning message. + * + * @param string $message + * @return $this + */ + public function warning($message) + { + $this->message($message, $this->levels['warning']); + + return $this; + } + + /** + * Create a message. + * + * @param string $message + * @param string $level + * @return $this + */ + public function message($message, $level = null) + { + if (!isset($level)) { + $level = $this->levels['default']; + } + + array_push($this->toasts, [ + 'message' => $message, + 'level' => $level + ]); + session()->flash('toasts', $this->toasts); + + return $this; + } + +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..d15e991 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,25 @@ +make('Grimthorr\LaravelToast\Toast'); + } + + if (!is_null($message)) { + return $instance->message($message); + } + + return $instance; + } +} diff --git a/views/messages-jquery.blade.php b/views/messages-jquery.blade.php new file mode 100644 index 0000000..d59d6ff --- /dev/null +++ b/views/messages-jquery.blade.php @@ -0,0 +1,18 @@ +@if(Session::has('toasts')) + + + + + + +@endif \ No newline at end of file diff --git a/views/messages.blade.php b/views/messages.blade.php new file mode 100644 index 0000000..f176947 --- /dev/null +++ b/views/messages.blade.php @@ -0,0 +1,9 @@ +@if(Session::has('toasts')) + @foreach(Session::get('toasts') as $toast) +
+ + + {{ $toast['message'] }} +
+ @endforeach +@endif \ No newline at end of file