2017 © Pedro Peláez
 

library laravel-sweetalert

A PHP package to show Sweet Alerts with the Laravel Framework

image

synergitech/laravel-sweetalert

A PHP package to show Sweet Alerts with the Laravel Framework

  • Wednesday, July 18, 2018
  • by forestlovewood
  • Repository
  • 4 Watchers
  • 0 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 124 Forks
  • 0 Open issues
  • 25 Versions
  • 0 % Grown

The README.md

Easy Sweet Alert Messages for Laravel

A success alert, (*1)

StyleCI, (*2)

Installation

First, pull in the package through Composer., (*3)

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

or faster using the terminal:, (*4)

    composer require uxweb/sweet-alert

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

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

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

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

Note that this package works only by using the BEAUTIFUL REPLACEMENT FOR JAVASCRIPT'S "ALERT"., (*7)

Finally, you need to get the Sweet Alert library, you can so by:, (*8)

Download the .js and .css from the website, (*9)

If you are using Laravel Elixir for your front-end workflow, add sweet alert with yarn or npm, (*10)

using Yarn:, (*11)

    yarn add sweetalert

using Npm:, (*12)

    npm install sweetalert

Usage

With the Facade

First import the Alert facade in your controller., (*13)

use Alert;

Within your controllers, before you perform a redirect..., (*14)

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

    return Redirect::home();
}
  • 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');

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:, (*15)

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

Like with the Facade we can use the helper with the same methods:, (*16)

  • 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..., (*17)

/**
 * 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');)., (*18)

With the Middleware

Using middleware groups

First register the middleware in web middleware groups by simply add the middleware class UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class into the $middlewareGroups of your app/Http/Kernel.php class:, (*19)

    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            ...
            \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

Ensure to register the middleware within 'web' group only., (*20)

Using route middleware

Or if you would like to assign the middleware to specific routes only, you should add the middleware to $routeMiddleware in app/Http/Kernel.php file:, (*21)

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    ....
    'sweetalert' => \UxWeb\SweetAlert\ConvertMessagesIntoSweetAlert::class,
];

Next step, Within your controllers, set your return message (using with()), send the proper message and proper type, (*22)

return redirect('dashboard')->with('success', 'Profile updated!'); 

or, (*23)

return redirect()->back()->with('errors', 'Profile updated!'); 

NOTE: When using the middleware it will make an alert to display if detects any of the following keys flashed into the session: errors, success, warning, info, message, basic., (*24)

The View

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:, (*25)

<!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>

REMEMBER: Always include the .css and .js files from the sweet-alert library., (*26)

Final Considerations

By default, all alerts will dismiss after a sensible default number of seconds., (*27)

But no fear, if you need to specify a different time you can:, (*28)

    // -> 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:, (*29)

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

You can render html in your message with the html() method like this:, (*30)

    // -> html will be evaluate
    alert('<a href="#">Click me</a>')->html()->persistent("No, thanks");

Customize

If you need to customize the alert message partial, run:, (*31)

    php artisan vendor:publish --provider "UxWeb\SweetAlert\SweetAlertServiceProvider"

The package view is located in the resources/views/vendor/sweet/ directory., (*32)

You can customize this view to fit your needs., (*33)

A sweet-alert.php configuration file will be published to your config directory as well, this will allow you to set the default timer for all autoclose alerts., (*34)

Configuration Options

You have access to the following configuration options to build a custom view:, (*35)

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., (*36)

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., (*37)

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

Custom View

@if (Session::has('sweet_alert.alert'))





@endif

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

Tests

To run the included test suite:, (*40)

vendor/bin/phpunit

Demo

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

return Redirect::home();

A simple alert, (*41)

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

return Redirect::home();

A simple alert with title, (*42)

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

return Redirect::home();

A simple alert with title and button, (*43)

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

return Redirect::home();

A info alert, (*44)

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

return Redirect::home();

A error alert, (*45)

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

return Redirect::home();

A success alert, (*46)

Alert::info('Random lorempixel.com : <img src="http://lorempixel.com/150/150/">')->html();

return Redirect::home();

HTML in message, (*47)

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

return Redirect::home();

A persistent alert, (*48)

The Versions

18/07 2018

dev-develop

dev-develop

A PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel notifier alert sweet

18/07 2018

0.0.2

0.0.2.0

A PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel notifier alert sweet

18/06 2018

0.0.1

0.0.1.0

A PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel notifier alert sweet

10/09 2017

dev-master

9999999-dev

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

10/09 2017

1.4.2

1.4.2.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

27/03 2017

1.4.1

1.4.1.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

23/11 2016

1.4.0

1.4.0.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

17/11 2016

1.3.1

1.3.1.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

15/08 2016

1.3.0

1.3.0.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

The Development Requires

by Uziel Bueno

laravel notifier alert sweet

12/08 2016

1.2.0

1.2.0.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

10/07 2016

1.1.6

1.1.6.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

29/05 2016

1.1.5

1.1.5.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

08/03 2016

1.1.4

1.1.4.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

22/12 2015

1.1.3

1.1.3.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

23/09 2015

1.1.2

1.1.2.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

21/09 2015

dev-L42

dev-L42

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

03/08 2015

1.1.1

1.1.1.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

01/08 2015

1.1.0

1.1.0.0

A simple PHP package to show Sweet Alerts with the Laravel Framework

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

15/07 2015

1.0.7

1.0.7.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

12/07 2015

1.0.6

1.0.6.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

12/07 2015

1.0.5

1.0.5.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

12/07 2015

1.0.4

1.0.4.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

laravel alert sweet

11/07 2015

1.0.2

1.0.2.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

11/07 2015

1.0.1

1.0.1.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno

11/07 2015

1.0.0

1.0.0.0

A simple PHP package to show SweetAlerts

  Sources   Download

MIT

The Requires

 

by Uziel Bueno