bilfeldt / laravel-flash-message
使用Laravel默认的会话消息闪现系统来闪现多个消息
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- illuminate/contracts: ^8.51 || ^9.0 || ^10.0 || ^11.0
Requires (Dev)
- nunomaduro/collision: ^5.3 || ^6.0 || ^7.0 || ^8.0
- orchestra/testbench: ^6.15 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3 || ^10.0
- spatie/laravel-ray: ^1.23
README
一个针对从后端闪现多个高级消息并在前端使用预构建的自定义Tailwind CSS alert blade组件显示这些消息的有见地的解决方案。
安装
通过Composer安装此包,然后您就可以添加消息并在前端显示这些消息了。
composer require bilfeldt/laravel-flash-message
现在您可以使用blade组件在前端显示消息。
可选:如果您希望使用消息闪现功能,允许消息在下一个请求中可用(在重定向组合中使用很有用),只需将ShareMessagesFromSession
中间件添加到app/Http/Kernel.php
中定义的web
组,紧接在ShareErrorsFromSession
中间件之后即可。
// app/Http/Kernel.php /** * The application's route middleware groups. * * @var array */ protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Laravel\Jetstream\Http\Middleware\AuthenticateSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \Bilfeldt\LaravelFlashMessage\Http\Middleware\ShareMessagesFromSession::class, // <------ ADDED HERE \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ...
用法
Alert blade组件
简单使用alert blade组件(message
,info
,success
,warning
,error
)
```blade
<x-flash::info
:title="__('Information')"
:text="__('This is a message which is relevant for you')"
:messages="[__('Bullet 1'), __('Bullet 2')]"
:links="[__('Read more') => 'https://example.com', __('Contact us') => 'https://example.com/contact']"
/>
验证错误
默认情况下,验证错误在Laravel中作为$errors
提供,并且可以使用以下方式轻松渲染这些错误:
<x-flash::errors :title="__('Validation errors')" :text="__('Please correct the following errors:')" />
从后端传递通知
此包最基本的使用方法是创建控制器中的消息并将其传递给视图。
<?php namespace App\Http\Controllers; use Bilfeldt\LaravelFlashMessage\Message; use Illuminate\Http\Request; class PostController extends Controller { /** * Display a listing of the resource. * * @param Request $request */ public function index(Request $request) { $message = Message::warning('This is a simple message intended for you') // message/success/info/warning/error ->title('This is important') ->addMessage('account', 'There is 10 days left of your free trial') ->addLink('Read more', 'https://example.com/signup'); return view('posts')->withMessage($message); } }
重定向
有时会返回重定向而不是视图给用户。在这种情况下,必须将消息闪现到会话中,以便它们在后续请求中可用。这可以通过简单地闪现$message
来完成。
<?php namespace App\Http\Controllers; use Bilfeldt\LaravelFlashMessage\Message; use Illuminate\Http\Request; class PostController extends Controller { /** * Display a listing of the resource. * * @param Request $request */ public function index(Request $request) { $message = Message::warning('This is a simple message intended for you') // message/success/info/warning/error ->title('This is important') ->addMessage('account', 'There is 10 days left of your free trial') ->addLink('Read more', 'https://example.com/signup'); return redirect('/posts')->withMessage($message); // This will flash the message to the Laravel session } }
请注意,为了使此功能正常工作,您需要将ShareMessagesFromSession
中间件添加到app/Http/Kernel.php
中,如上面安装部分中所述。
在需要将消息闪现到会话中但没有访问重定向的情况,例如在Laravel Livewire组件中,我们添加了一个小的辅助方法session_message($message)
,它执行相同的操作。
从代码的任何位置添加消息
可以在代码库的任何地方基本添加消息,使用View
外观。尽管大多数用例将是将消息从控制器中添加,但此功能可以非常强大,例如与中间件结合使用。
\Illuminate\Support\Facades\View::withMessage($message);
多个消息包
可能存在需要多个“消息包”(与Laravel用于验证消息的方法相同)的情况,在这种情况下,可以按如下方式命名包:
return view('posts')->withMessage($message, 'bag-name'); // or return redirect('/posts')->withMessage($message, 'bag-name');
显示消息时,只需传递包名称即可
<x-flash::messages bag='bag-name' />
提示
您可能有一个布局,您希望始终在主内容上方或标题下方闪现消息。您只需将<x-flash::messages />
添加到布局文件中您希望消息显示的位置即可,这样您就不需要在多个视图中调用它。为了避免在未应用ShareMessagesFromSession
时$messages
未设置的问题,建议按如下方式显示消息:
@isset($messages) <x-flash::messages /> @endif
如果您需要在特定位置显示特定消息,只需为这些消息使用命名消息包并显示在所需位置即可。
替代品/补充
此包在处理刀片文件以及在前端渲染期间从后端传递消息时非常有用。如果您在寻找吐司(弹出)消息,请查看这些出色的包
测试
composer test
变更日志
请参阅变更日志,了解最近发生了哪些变化。
致谢
- Anders Bilfeldt:主要包开发者
- iAmine:警报刀片组件
- Showcode.app:创建终端模拟的服务
- Beyond Code:横幅生成器
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。