You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Contributors and Sponsors](#contributors-and-sponsors)
40
+
-[Contact](#contact)
41
+
-[License](#license)
42
+
43
+
## About PHPFlasher Symfony Adapter
44
+
45
+
**PHPFlasher Symfony Adapter** is an open-source package that seamlessly integrates PHPFlasher’s robust flash messaging capabilities into your **Symfony** applications. It streamlines the process of adding flash messages, offering an intuitive API to enhance user experience with minimal configuration.
46
+
47
+
With PHPFlasher Symfony Adapter, you can effortlessly display success, error, warning, and informational messages to your users, ensuring clear communication of application states and actions.
48
+
49
+
## Features
50
+
51
+
-**Seamless Symfony Integration**: Designed specifically for Symfony, ensuring compatibility and ease of use.
52
+
-**Multiple Notification Libraries**: Supports various frontend libraries like Toastr, Noty, SweetAlert, and Notyf.
53
+
-**Flexible Configuration**: Customize the appearance and behavior of flash messages to fit your application's needs.
54
+
-**Intuitive API**: Simple methods to create and manage flash messages without boilerplate code.
55
+
-**Extensible**: Easily add or create new adapters for different frontend libraries.
56
+
57
+
## Supported Versions
58
+
59
+
| PHPFlasher Symfony Adapter Version | PHP Version | Symfony Version |
> **Note:** Ensure your project meets the PHP and Symfony version requirements for the PHPFlasher Symfony Adapter version you intend to use. For older PHP or Symfony versions, refer to [PHPFlasher v1.x](https://github.com/php-flasher/flasher-symfony/tree/1.x).
65
+
66
+
## Installation
67
+
68
+
### Core Package
69
+
70
+
Install the PHPFlasher Symfony Adapter via Composer:
71
+
72
+
```bash
73
+
composer require php-flasher/flasher-symfony
74
+
```
75
+
76
+
After installation, set up the necessary assets:
77
+
78
+
```shell
79
+
php bin/console flasher:install
80
+
```
81
+
82
+
> **Note:** PHPFlasher automatically injects the necessary JavaScript and CSS assets into your Blade templates. No additional steps are required for asset injection.
83
+
84
+
### Adapters
85
+
86
+
PHPFlasher provides various adapters for different notification libraries. Below is an overview of available adapters for Symfony:
|`default`|**String**: The default notification library to use (e.g., `'flasher'`, `'toastr'`, `'noty'`, `'notyf'`, `'sweetalert'`). |
165
+
|`main_script`|**String**: Path to the main PHPFlasher JavaScript file. |
166
+
|`styles`|**Array**: List of CSS files to style your notifications. |
167
+
|`options`|**Array** (Optional): Global options for all notifications (e.g., `'timeout'`, `'position'`). |
168
+
|`inject_assets`|**Boolean**: Whether to automatically inject JavaScript and CSS assets into your HTML pages. |
169
+
|`translate`|**Boolean**: Enable message translation using Symfony’s translation service. |
170
+
|`excluded_paths`|**Array**: URL patterns to exclude from asset injection and flash_bag conversion. |
171
+
|`flash_bag`|**Array**: Map Symfony flash message keys to notification types. |
172
+
|`filter`|**Array** (Optional): Criteria to filter which notifications are displayed (e.g., `'limit'`). |
173
+
|`presets`|**Array** (Optional): Define notification presets to simplify notification creation. |
174
+
175
+
## Quick Start
176
+
177
+
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.
178
+
179
+
### Using the `flash()` Helper
180
+
181
+
```php
182
+
<?php
183
+
184
+
namespace App\Controller;
185
+
186
+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
187
+
use Symfony\Component\HttpFoundation\RedirectResponse;
188
+
189
+
class BookController extends AbstractController
190
+
{
191
+
public function saveBook(): RedirectResponse
192
+
{
193
+
// Your logic here
194
+
195
+
flash('Your changes have been saved!');
196
+
197
+
return $this->redirectToRoute('book_list');
198
+
}
199
+
}
200
+
```
201
+
202
+
### Using the `flasher` Service
203
+
204
+
```php
205
+
<?php
206
+
207
+
namespace App\Controller;
208
+
209
+
use Flasher\Prime\FlasherInterface;
210
+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
211
+
use Symfony\Component\HttpFoundation\RedirectResponse;
212
+
213
+
class AnotherController extends AbstractController
214
+
{
215
+
private FlasherInterface $flasher;
216
+
217
+
public function __construct(FlasherInterface $flasher)
218
+
{
219
+
$this->flasher = $flasher;
220
+
}
221
+
222
+
public function register(): RedirectResponse
223
+
{
224
+
// Your logic here
225
+
226
+
$this->flasher->success('Your changes have been saved!');
227
+
228
+
// ... redirect or render the view
229
+
return $this->redirectToRoute('home');
230
+
}
231
+
232
+
public function update(): RedirectResponse
233
+
{
234
+
// Your logic here
235
+
236
+
$this->flasher->error('An error occurred while updating.');
flash()->info('This is an informational message.');
261
+
```
262
+
263
+
### Warning Message
264
+
265
+
```php
266
+
flash()->warning('This is a warning message.');
267
+
```
268
+
269
+
### Passing Options
270
+
271
+
```php
272
+
flash()->success('Custom message with options.', ['timeout' => 3000, 'position' => 'bottom-left']);
273
+
```
274
+
275
+
### Using presets
276
+
277
+
Define a preset in your `config/packages/flasher.yaml`:
278
+
279
+
```yaml
280
+
flasher:
281
+
# ... other configurations
282
+
283
+
presets:
284
+
entity_saved:
285
+
type: 'success'
286
+
title: 'Entity Saved'
287
+
message: 'The entity has been saved successfully.'
288
+
entity_deleted:
289
+
type: 'warning'
290
+
title: 'Entity Deleted'
291
+
message: 'The entity has been deleted.'
292
+
```
293
+
294
+
Use the preset in your controller:
295
+
296
+
```php
297
+
<?php
298
+
299
+
namespace App\Controller;
300
+
301
+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
302
+
use Symfony\Component\HttpFoundation\RedirectResponse;
303
+
304
+
class BookController extends AbstractController
305
+
{
306
+
public function save(): RedirectResponse
307
+
{
308
+
// Your saving logic
309
+
310
+
flash()->preset('entity_saved');
311
+
312
+
return $this->redirectToRoute('books.index');
313
+
}
314
+
315
+
public function delete(): RedirectResponse
316
+
{
317
+
// Your deletion logic
318
+
319
+
flash()->preset('entity_deleted');
320
+
321
+
return $this->redirectToRoute('books.index');
322
+
}
323
+
}
324
+
```
15
325
16
-
PHPFlasher is a powerful and easy-to-use package that allows you to quickly and easily add flash messages to your Laravel or Symfony projects.
17
-
Whether you need to alert users of a successful form submission, an error, or any other important information, flash messages are a simple and effective solution for providing feedback to your users.
326
+
## Adapters Overview
18
327
19
-
With PHPFlasher, you can easily record and store messages within the session, making it simple to retrieve and display them on the current or next page.
20
-
This improves user engagement and enhances the overall user experience on your website or application.
328
+
PHPFlasher supports various adapters to integrate seamlessly with different frontend libraries. Below is an overview of available adapters for Symfony:
21
329
22
-
Whether you're a beginner or an experienced developer, PHPFlasher's intuitive and straightforward design makes it easy to integrate into your projects.
23
-
So, if you're looking for a reliable, flexible and easy to use flash messages solution, PHPFlasher is the perfect choice.
|[flasher-toastr-symfony](https://github.com/php-flasher/flasher-toastr-symfony)| Toastr adapter for Symfony |
334
+
|[flasher-noty-symfony](https://github.com/php-flasher/flasher-noty-symfony)| Noty adapter for Symfony |
335
+
|[flasher-notyf-symfony](https://github.com/php-flasher/flasher-notyf-symfony)| Notyf adapter for Symfony |
336
+
|[flasher-sweetalert-symfony](https://github.com/php-flasher/flasher-sweetalert-symfony)| SweetAlert adapter for Symfony |
24
337
338
+
> **Note:** Each adapter has its own repository. For detailed installation and usage instructions, please refer to the [Official Documentation](https://php-flasher.io).
25
339
26
340
## Official Documentation
27
341
28
-
Documentation for PHPFlasher can be found on the [https://php-flasher.io](https://php-flasher.io).
342
+
Comprehensive documentation for PHPFlasher is available at [https://php-flasher.io](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.
29
343
30
344
## Contributors and sponsors
31
345
@@ -42,7 +356,7 @@ Shining stars of our community:
0 commit comments