一个轻量级的包,用于在Laravel应用程序中发送Flash消息

网友投稿 571 2022-10-30

一个轻量级的包,用于在Laravel应用程序中发送Flash消息

一个轻量级的包,用于在Laravel应用程序中发送Flash消息

A lightweight package to flash messages

This is a lightweight package to send flash messages in Laravel apps. A flash message is a message that is carried over to the next request by storing it in the session. This package only supports one single flash message at a time.

This is how it can be used:

class MySpecialSnowflakeController{ public function store() { // … flash('My message', 'my-class'); return back(); }}

In your view you can do this:

@if (flash()->message)

{{ flash()->message }}
@endif

Support us

Learn how to create a package like this one, by watching our premium video course:

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-flash

Usage

Here is an example on how to flash a message.

class MyController{ public function store() { // … flash('My message'); return back(); }}

In your view you can use it like this

@if(flash()->message)

{{ flash()->message }}
@endif

Using a class name to style the displayed message

You can add a class as the second parameter. This is typically used to style the output in your HTML.

class MyController{ public function store() { // … flash('My message', 'my-class'); return back(); }}

In your view you can use the class like this:

@if (flash()->message)

{{ flash()->message }}
@endif

You can also set an array of classes. These will be output by flash()->class by imploding the array with a space-delimiter.

flash('My message', ['my-class', 'another-class']); // flash()->class output is: 'my-class another-class'

Adding your own methods

If you don't want to specify a class each time you flash a message you can add a method name to flash.

The easiest way is by passing an array to the levels method. The key is the method name that should be added to flash(). The value is the class that will automatically be used when rendering the message.

// this would probably go in a service provider\Spatie\Flash\Flash::levels([ 'success' => 'alert-success', 'warning' => 'alert-warning', 'error' => 'alert-error',]);

The above example will make these methods available on flash:

flash()->success('Hurray');flash()->warning('Mayybeee');flash()->error('Oh Oh');

The most likely scenario is that you want to consume the flash message in a view. You can use the message, class and level properties on the view.

@if (flash()->message)

{{ flash()->message }}
@if(flash()->level === 'error') This was an error. @endif@endif

Additionally, when you've added your own method, you can also pass that method name as a second parameter to flash itself:

flash('Hurray', 'success'); // `flash()->class` will output 'alert-success'

You can also add a method to flash by using macro.

Here's an example:

// this would probably go in a service provideruse Spatie\Flash\Message;\Spatie\Flash\Flash::macro('warning', function (string $message) { return $this->flashMessage(new Message($message, 'alert alert-warning'));});

You can now use a warning method on flash:

flash()->warning('Look above you!');

You can pass the desired level as the third argument to Message.

// in a service provideruse Spatie\Flash\Message;\Spatie\Flash\Flash::macro('warning', function (string $message) { return $this->flashMessage(new Message($message, 'alert alert-warning', 'my-level'));});// in the next request, after having flashed a message flash()->level; // returns `my-level`

Alternatives

This package is intended to be lightweight. If you need things like multiple messages, support for Bootstrap, overlays, ... take a look at this excellent flash package by Jeffrey Way or Laraflash by Ilya Sakovich.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.

Credits

Freek Van der HertenAll Contributors

License

The MIT License (MIT). Please see License File for more information.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:hdu4499 搜索
下一篇:AUC / uAUC
相关文章

 发表评论

暂时没有评论,来抢沙发吧~