germey/sweetalert

在 Laravel 上使用的 Sweet Alert 扩展

dev-master 2016-07-29 17:07 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:03:59 UTC


README

安装

首先,通过 Composer 引入此包。

"require": {
    "germey/sweetalert": "dev-master"
}

如果使用 Laravel 5,在 config/app.php 文件中包含服务提供者。

'providers' => [
    Germey\SweetAlert\SweetAlertServiceProvider::class
];

为了方便,在文件底部添加一个门面别名

'aliases' => [
    'SweetAlert' => Germey\SweetAlert\SweetAlert::class
];

使用方法

使用门面

在控制器中,在执行重定向之前...

public function store()
{
    SweetAlert::message('Robots are working!');

    return Redirect::home();
}

使用助手函数

  • alert($message = null, $title = '')

除了前面列出的方法外,您还可以直接使用助手函数而不指定任何消息类型。这样做类似于

  • alert()->message('Message', 'Optional Title')

与门面一样,我们可以使用相同的辅助方法

alert()->message('Message', 'Optional Title');
alert()->basic('Basic Message', 'Mandatory Title');
alert()->info('Info Message', 'Optional Title');
alert()->success('Success Message', 'Optional Title');
alert()->error('Error Message', 'Optional Title');
alert()->warning('Warning Message', 'Optional Title');

alert()->basic('Basic Message', 'Mandatory Title')
    ->autoclose(3500);

alert()->error('Error Message', 'Optional Title')
    ->persistent('Close');

在控制器中,在执行重定向之前...

/**
 * Destroy the user's session (logout).
 *
 * @return Response
 */
public function destroy()
{
    Auth::logout();

    alert()->success('You have been logged out.', 'Good bye!');

    return home();
}

对于一般信息提示,只需这样做: alert('Some message');(等同于 alert()->message('Some message');)。

默认情况下,所有提示将在合理的默认秒数后消失。

但不必担心,如果您需要指定不同的时间,可以

    // -> Remember!, the number is set in milliseconds
    alert('Hello World!')->autoclose(3000);

此外,如果您需要提示在页面上持续存在,直到用户按下提示确认按钮将其关闭

    // -> The text will appear in the button
    alert('Hello World!')->persistent("Close this");

最后,要在浏览器中显示提示,您可以使用(或修改)此包中包含的视图。只需将其包含到布局视图中即可

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/sweetalert.css">
</head>
<body>

    <div class="container">
        <p>Welcome to my website...</p>
    </div>

    <script src="js/sweetalert.min.js"></script>

    <!-- Include this after the sweet alert js file -->
    @include('sweetalert::alert')

</body>
</html>