php-flasher / flasher-laravel
Seamlessly integrate flash notifications into your Laravel applications with PHPFlasher. Enhance user feedback and engagement with minimal setup.
Fund package maintenance!
yoeunes
Patreon
Ko Fi
Open Collective
www.paypal.com/paypalme/yoeunes
Requires
- php: >=8.2
- illuminate/support: ^11.0
- php-flasher/flasher: ^2.1.1
- dev-main
- 2.x-dev
- v2.1.1
- v2.1.0
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- 1.x-dev
- v1.15.14
- v1.15.13
- v1.15.12
- v1.15.11
- v1.15.10
- v1.15.9
- v1.15.8
- v1.15.7
- v1.15.6
- v1.15.5
- v1.15.4
- v1.15.3
- v1.15.2
- v1.15.1
- v1.15.0
- v1.14.1
- v1.14.0
- v1.13.1
- v1.13.0
- v1.12.1
- v1.12.0
- v1.11.0
- v1.10.6
- v1.10.5
- v1.10.4
- v1.10.3
- v1.10.2
- v1.10.1
- v1.10.0
- v1.9.2
- v1.9.1
- v1.9.0
- v1.8.2
- v1.8.1
- v1.8.0
- v1.7.0
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.1
- v1.3.0
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.9.8
- v0.9.7
- v0.9.6
- v0.9.5
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.17
- v0.4.16
- v0.4.15
- v0.4.14
- v0.4.13
- v0.4.12
- v0.4.11
- v0.4.10
- v0.4.8
- v0.4.7
- v0.4.6
- v0.4.5
- v0.4.4
- v0.4.3
- v0.4.2
- v0.4.1
- v0.4.0
- v0.3.0
- v0.2.0
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
This package is auto-updated.
Last update: 2024-11-27 15:22:49 UTC
README
Table of Contents
- About PHPFlasher Laravel Adapter
- Features
- Supported Versions
- Installation
- Configuration
- Quick Start
- Usage Examples
- Adapters Overview
- Official Documentation
- Contributors and Sponsors
- Contact
- License
About PHPFlasher Laravel Adapter
PHPFlasher Laravel Adapter is an open-source package that seamlessly integrates PHPFlasher's powerful flash messaging capabilities into your Laravel applications. It simplifies the process of adding flash messages, providing an intuitive API to enhance user experience with minimal setup.
With PHPFlasher Laravel Adapter, you can effortlessly display success, error, warning, and informational messages to your users, ensuring clear communication of application states and actions.
Features
- Seamless Laravel Integration: Designed specifically for Laravel, ensuring compatibility and ease of use.
- Multiple Notification Libraries: Supports various frontend libraries like Toastr, Noty, SweetAlert, and Notyf.
- Flexible Configuration: Customize the appearance and behavior of flash messages to fit your application's needs.
- Intuitive API: Simple methods to create and manage flash messages without boilerplate code.
- Extensible: Easily add or create new adapters for different frontend libraries.
Supported Versions
Note: Ensure your project meets the PHP and Laravel version requirements for the PHPFlasher Laravel Adapter version you intend to use. For older PHP or Laravel versions, refer to PHPFlasher v1.x.
Installation
Core Package
Install the PHPFlasher Laravel Adapter via Composer:
composer require php-flasher/flasher-laravel
After installation, set up the necessary assets:
php artisan flasher:install
Note: PHPFlasher automatically injects the necessary JavaScript and CSS assets into your Blade templates. No additional steps are required for asset injection.
Adapters
PHPFlasher provides various adapters for different notification libraries. Below is an overview of available adapters for Laravel:
- flasher-toastr-laravel - Laravel Adapter
- flasher-noty-laravel - Laravel Adapter
- flasher-notyf-laravel - Laravel Adapter
- flasher-sweetalert-laravel - Laravel Adapter
For detailed installation and usage instructions for each adapter, refer to their respective README.md
.
Configuration
After installing the PHPFlasher Laravel Adapter, you can configure it by publishing the configuration file or by modifying it directly.
Configuration File
If you need to customize the default settings, publish the configuration file using the following command:
php artisan flasher:install --config
This will create a file at config/flasher.php
with the following content:
<?php declare(strict_types=1); use Flasher\Prime\Configuration; return Configuration::from([ // Default notification library (e.g., 'flasher', 'toastr', 'noty', 'notyf', 'sweetalert') 'default' => 'flasher', // Path to the main PHPFlasher JavaScript file 'main_script' => '/vendor/flasher/flasher.min.js', // List of CSS files to style your notifications 'styles' => [ '/vendor/flasher/flasher.min.css', ], // Set global options for all notifications (optional) // 'options' => [ // 'timeout' => 5000, // Time in milliseconds before the notification disappears // 'position' => 'top-right', // Where the notification appears on the screen // ], // Automatically inject JavaScript and CSS assets into your HTML pages 'inject_assets' => true, // Enable message translation using Laravel's translation service 'translate' => true, // URL patterns to exclude from asset injection and flash_bag conversion 'excluded_paths' => [], // Map Laravel flash message keys to notification types 'flash_bag' => [ 'success' => ['success'], 'error' => ['error', 'danger'], 'warning' => ['warning', 'alarm'], 'info' => ['info', 'notice', 'alert'], ], // Set criteria to filter which notifications are displayed (optional) // 'filter' => [ // 'limit' => 5, // Maximum number of notifications to show at once // ], // Define notification presets to simplify notification creation (optional) // 'presets' => [ // 'entity_saved' => [ // 'type' => 'success', // 'title' => 'Entity saved', // 'message' => 'Entity saved successfully', // ], // ], ]);
Configuration Options
Quick Start
To display a notification message, you can either use the flash()
helper function or obtain an instance of flasher
from the service container. Then, before returning a view or redirecting, call the desired method (success()
, error()
, etc.) and pass in the message to be displayed.
Using the flash()
Helper
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class BookController extends Controller { public function saveBook() { // Your logic here flash('Your changes have been saved!'); return redirect()->back(); } }
Using the flasher
Service
<?php namespace App\Http\Controllers; use Flasher\Prime\FlasherInterface; class AnotherController extends Controller { /** * If you prefer to use dependency injection */ public function register(FlasherInterface $flasher) { // Your logic here $flasher->success('Your changes have been saved!'); // ... redirect or render the view } public function update() { // Your logic here app('flasher')->error('An error occurred while updating.'); return redirect()->back(); } }
Usage Examples
Success Message
flash()->success('Operation completed successfully!');
Error Message
flash()->error('An error occurred.');
Info Message
flash()->info('This is an informational message.');
Warning Message
flash()->warning('This is a warning message.');
Passing Options
flash()->success('Custom message with options.', ['timeout' => 3000, 'position' => 'bottom-left']);
Using presets
Define a preset in your config/flasher.php
:
<?php declare(strict_types=1); use Flasher\Prime\Configuration; return Configuration::from([ // ... other configurations 'presets' => [ 'entity_saved' => [ 'type' => 'success', 'title' => 'Entity Saved', 'message' => 'The entity has been saved successfully.', ], 'entity_deleted' => [ 'type' => 'warning', 'title' => 'Entity Deleted', 'message' => 'The entity has been deleted.', ], ], ]);
Use the preset in your controller:
<?php namespace App\Http\Controllers; use Illuminate\Http\RedirectResponse; use App\Http\Controllers\Controller; class BookController extends Controller { /** * Save a new book entity. * * @return RedirectResponse */ public function saveBook(): RedirectResponse { // Your saving logic here (e.g., validating and storing the book) // Trigger the 'entity_saved' preset flash()->preset('entity_saved'); return redirect()->route('books.index'); } /** * Delete an existing book entity. * * @return RedirectResponse */ public function deleteBook(): RedirectResponse { // Your deletion logic here (e.g., finding and deleting the book) // Trigger the 'entity_deleted' preset flash()->preset('entity_deleted'); return redirect()->route('books.index'); } }
Adapters Overview
PHPFlasher supports various adapters to integrate seamlessly with different frontend libraries. Below is an overview of available adapters for Laravel:
Note: Each adapter has its own repository. For detailed installation and usage instructions, please refer to the Official Documentation.
Official Documentation
Comprehensive documentation for PHPFlasher is available at https://php-flasher.io. Here you will find detailed guides, API references, and advanced usage examples to help you get the most out of PHPFlasher.
Contributors and sponsors
Join our team of contributors and make a lasting impact on our project!
We are always looking for passionate individuals who want to contribute their skills and ideas. Whether you're a developer, designer, or simply have a great idea, we welcome your participation and collaboration.
Shining stars of our community:
Contact
PHPFlasher is being actively developed by yoeunes. You can reach out with questions, bug reports, or feature requests on any of the following:
License
PHPFlasher is open-sourced software licensed under the MIT license.
Made with ❤️ by Younes ENNAJI