hadefication / siren
此软件包的最新版本(v1.0.2)没有可用的许可证信息。
v1.0.2
2017-04-23 16:28 UTC
Requires
- illuminate/support: ^5.4
This package is auto-updated.
Last update: 2024-09-24 21:07:13 UTC
README
用于 Laravel 的 toast 风格应用消息,使用 toastr 实现。。
安装
- 运行
composer require hadefication/siren
进行安装。 - 将服务提供者和外观添加到您的
config/app.php
。
'providers' => [
...
Hadefication\Siren\SirenServiceProvider::class,
...
],
'aliases' = [
...
'Siren' => Hadefication\Siren\SirenFacade::class,
...
]
使用
- 要收集消息,您可以使用外观
Siren
或名为siren()
的小巧助手函数。这将使您能够访问 siren 提供的所有方法。要收集错误消息,请使用siren()->error($message, $title, $important)
,其中$message
显然是要传达的消息,同样$title
也适用,然后$important
将确定您的消息是否在关闭前保持显示(设置为true
)或自动在几秒钟后关闭。以下是一个使用 siren 收集的消息类型的示例。
// Example
$message = 'This is a message';
$title = 'Notification';
$important = false;
// Error
siren()->error($message, $title, $important);
// or
\Siren::error($message, $title, $important);
// Success
siren()->success($message, $title, $important);
// or
\Siren::success($message, $title, $important);
// Warning
siren()->warning($message, $title, $important);
// or
\Siren::warning($message, $title, $important);
// Notice or Info
siren()->notice($message, $title, $important);
// or
\Siren::notice($message, $title, $important);
- 接下来,您需要通过将其添加到
app.js
或直接添加到您的视图中来包含相应的 JavaScript 库。如果您选择后者,可能需要将vendor/src/resources/assets/dist/siren.js
复制到您的公开路径。幸运的是,Laravel Elixir 或 Laravel Mix 可以为您完成这项工作!一旦添加了 JavaScript 库,您将可以使用siren
全局变量,有关更多信息,请参阅 JavaScript 部分。
// app.js
require('vendor/src/resources/assets/dist/siren.js');
// direct
<script src="{{ asset('path/to/siren.js') }}"></script>
- 要显示 toast 消息,您需要在布局的视图末尾添加
siren()->render()
或\Siren::render()
。重要的是,此方法将是最后调用的方法。
{!! siren()->render() !!}
// or
{!! \Siren::render() !!}
JavaScript
配套的 JavaScript 库使用名为 toastr 的酷插件来渲染 toast 风格的消息。一旦引用了 JavaScript 库,您将能够访问 siren
变量,此变量将提供访问库所提供所有方法的权限。
siren.setDefaults(options)
- 允许您覆盖 toastr 的默认设置。参数options
将是您想要覆盖的设置。
// Overridable options
{
closeButton: true,
debug: false,
newestOnTop: true,
progressBar: true,
positionClass: "toast-top-right",
preventDuplicates: true,
showDuration: 300,
hideDuration: 1000,
timeOut: 5000,
extendedTimeOut: 1000,
showEasing: "swing",
hideEasing: "linear",
showMethod: "fadeIn",
hideMethod: "fadeOut"
}
// Example
// Override close button to be hidden
siren.setDefaults({
closeButton: false,
})
siren.error(message, title, important, options)
- 将显示一个错误 toast 消息。参数message
显然是要显示的消息,同样title
也适用,然后important
将是您想要消息保持显示直到关闭或自动在一段时间后关闭的标志,最后参数options
将提供“选项”以覆盖 toastr 的默认设置。对于其他消息类型也是如此。以下是一个更详细的示例。
// Error
// Will show close button to the toast message and will show the toast for 10 second
siren.error('This is a test', 'Error', false, {closeButton: true, timeOut: 10000});
// Success
siren.success('This is a success message', 'Success');
// Warning
siren.warning('Warning message here', 'Warning!');
// Notice
siren.success('A notice message here', 'Notification', true);