GithubHelp home page GithubHelp logo

zadra / sweet-alert Goto Github PK

View Code? Open in Web Editor NEW

This project forked from uxweb/sweet-alert

0.0 2.0 0.0 51 KB

A simple PHP package to show SweetAlerts with the Laravel Framework

PHP 96.72% HTML 3.28%

sweet-alert's Introduction

Easy Sweet Alert Messages for Laravel

A success alert

StyleCI

Installation

First, pull in the package through Composer.

"require": {
    "uxweb/sweet-alert": "~1.1"
}

If using Laravel 5, include the service provider within config/app.php.

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

And, for convenience, add a facade alias to this same file at the bottom:

'aliases' => [
    'Alert' => UxWeb\SweetAlert\SweetAlert::class
];

Note that this package works only by using the BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT".

Finally, you need to get the Sweet Alert library, you can so by:

Download the .js and .css from the website

Or through bower:

bower install sweetalert

Usage

With the Facade

You first need to specify Alert in your controller.

use Alert;
  • 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');

Within your controllers, before you perform a redirect...

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

    return Redirect::home();
}

With the Helper

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

In addition to the previous listed methods you can also just use the helper function without specifying any message type. Doing so is similar to:

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

Like with the Facade we can use the helper with the same methods:

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');

Within your controllers, before you perform a redirect...

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

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

    return home();
}

For a general information alert, just do: alert('Some message'); (same as alert()->message('Some message');).

By default, all alerts will dismiss after a sensible default number of seconds.

But no fear, if you need to specify a different time you can:

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

Also, if you need the alert to be persistent on the page until the user dismiss it by pressing the alert confirmation button:

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

Finally, to display the alert in the browser, you may use (or modify) the view that is included with this package. Simply include it to your layout view:

<!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('sweet::alert')

</body>
</html>

Customize

If you need to customize the alert message partial, run:

    php artisan vendor:publish

The package view is located in the resources/views/vendor/sweet/ directory.

You can override/overwrite your sweet alert configuration to fit your needs.

Configuration Options

You have access to the following configuration options to build a custom view:

Session::get('sweet_alert.text')
Session::get('sweet_alert.type')
Session::get('sweet_alert.title')
Session::get('sweet_alert.confirmButtonText')
Session::get('sweet_alert.showConfirmButton')
Session::get('sweet_alert.allowOutsideClick')
Session::get('sweet_alert.timer')

Please check the CONFIGURATION section in the website for all other options available.

Default View

@if (Session::has('sweet_alert.alert'))
    <script>
        swal({!! Session::get('sweet_alert.alert') !!});
    </script>
@endif

The sweet_alert.alert session key contains a JSON configuration object to pass it directly to Sweet Alert.

Note that {!! !!} are used to output the json configuration object unescaped, it will not work with {{ }} escaped output tags.

Custom View

@if (Session::has('sweet_alert.alert'))
    <script>
        swal({
            text: "{!! Session::get('sweet_alert.text') !!}",
            title: "{!! Session::get('sweet_alert.title') !!}",
            timer: {!! Session::get('sweet_alert.timer') !!},
            type: "{!! Session::get('sweet_alert.type') !!}",
            showConfirmButton: "{!! Session::get('sweet_alert.showConfirmButton') !!}",
            confirmButtonText: "{!! Session::get('sweet_alert.confirmButtonText') !!}",
            confirmButtonColor: "#AEDEF4"

            // more options
        });
    </script>
@endif

Note that you must use "" (double quotes) to wrap the values except for the timer option.

Demo

Alert::message('Welcome back!');

return Redirect::home();

A simple alert

Alert::message('Your profile is up to date', 'Wonderful!');

return Redirect::home();

A simple alert with title

Alert::message('Thanks for comment!')->persistent('Close');

return Redirect::home();

A simple alert with title and button

Alert::info('Email was sent!');

return Redirect::home();

A info alert

Alert::error('Something went wrong', 'Oops!');

return Redirect::home();

A error alert

Alert::success('Good job!');

return Redirect::home();

A success alert

Alert::success('Good job!')->persistent("Close");

return Redirect::home();

A persistent alert

sweet-alert's People

Contributors

uxweb avatar jrean avatar khaledsmq avatar georgeboot avatar julientant avatar mmonbr avatar djsigfried56 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.