Home > PHP Framework > Laravel > Laravel message prompt extension recommendation: spatie/Laravel-flash

Laravel message prompt extension recommendation: spatie/Laravel-flash

青灯夜游
Release: 2022-11-29 21:08:07
forward
1851 people have browsed it

This article will share with you a lightweight message prompt Laravel extension: spatie/Laravel-flash package, and introduce how to use it in Laravel. I hope it will be helpful to everyone!

Laravel message prompt extension recommendation: spatie/Laravel-flash

For the past few years, we have been using the laracasts/flash package to flash messages in our projects. A flash message is a message that can only be passed from the current request to the next request. Laracasts does this very well. It supports multiple types of flash messages, overlay messages. It has boot styles and preset message levels out of the box.

We found that we only used a small part of the functions of the laracasts/flash package in the project. So we launched a lightweight package spatie/laravel-flash. In this blog I will introduce it to you.
Our flash package can only send one flash message at a time. Since only this functionality is needed, we can keep the API very clean. Next is an example of how to use it:

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

Let’s take a look at how to display flash messages. We have no views in the package. You can create a partial view yourself in your application. The content of the view may look like this:

@if(flash()->message)
    <div class="{{ flash()->class }}">
        {{ flash()->message }}
    </div>
@endif
Copy after login

Very simple, right?
You may have noticed that in the above php example we can specify a specific CSS class name-my-class as the second parameter of the flash function. In actual applications you may need to specify whether the message is a success, warning or error message, rather than specifying a specific class name. This package makes it easy to define message levels and associate them with CSS classes. Next is how to use it:

// 这个会放在一个 service provider 中
\Spatie\Flash\Flash::levels([
    'success' => 'alert-success',
    'warning' => 'alert-warning',
    'error' => 'alert-eror',
]);
Copy after login

Now you can set a flash message:

flash()->success('My message');
// 替代做法
flash('My message', 'success');
Copy after login

flash()->class will be returned in the next request alert-success.
That’s all the contents of this package. We intend to keep this package lightweight as we have a lot of functionality in flash messaging that we simply don't need. I hope the spatie/laravel-flash package can also be helpful in your project.

Original address: https://murze.be/a-laravel-package-to-flash-messages

Translation address: https://learnku.com/laravel/ t/25990

[Related recommendations: laravel video tutorial]

The above is the detailed content of Laravel message prompt extension recommendation: spatie/Laravel-flash. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template