tjgazel / laravel-toastr
适用于laravel 5.5+、6和7的通知Toastr
Requires
- php: >=7.0
Requires (Dev)
- illuminate/session: ^5.5.0
- illuminate/support: ^5.5.0
This package is auto-updated.
Last update: 2024-09-04 09:12:21 UTC
README
其他包
- tjgazel/laravel-bs4-alert - 适用于laravel 5.*的Bootstrap 4警报
- tjgazel/laravel-bs3-alert - 适用于laravel 5.*的Bootstrap 3警报
安装
1) 安装Toastr库(前端依赖)
1.1) 使用npm安装Toastr。 Github | 演示
npm install toastr --save-dev
1.2) 将js要求添加到资源/资产/js/bootstrap.js中,作为window.toastr = require('toastr');
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap');
window.toastr = require('toastr');
} catch (e) { }
1.3) 将sass导入资源/资产/sass/app.scss中,作为@import "~toastr/toastr";
然后通过npm npm run prod
构建。
// Fonts
@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700");
// Variables
@import "variables";
// Bootstrap
@import "~bootstrap/scss/bootstrap";
// Toastr
@import "~toastr/toastr";
2) 安装tjgazel/laravel-toastr
2.1) 运行
composer require tjgazel/laravel-toastr
2.2) 可选: Laravel 5.4及以下
将TJGazel\Toastr\ToastrServiceProvider::class
添加到config/app.php
中的providers
。
将'Toastr' => TJGazel\Toastr\Facades\Toastr::class
添加到config/app.php
中的aliases
。
// config/app.php
'providers' => [
// ...
TJGazel\Toastr\ToastrServiceProvider::class,
],
'aliases' => [
// ...
'Toastr' => TJGazel\Toastr\Facades\Toastr::class,
],
2.3) 运行php artisan vendor:publish --provider="TJGazel\Toastr\ToastrServiceProvider"
以发布config/toastr.php
中的配置文件
2.4) 在主视图中添加!! Toastr::render() !!}
Facade或!! toastr()->render() !!}
辅助函数
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="/css/app.css">
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="js/app.js"></script>
{!! toastr()->render() !!}
</body>
</html>
2.5) 可选: 修改位于config/toastr.php
的发布配置文件以符合您的喜好。
<?php
return [
'session_name' => 'toastr',
'options' => [
"closeButton" => true,
"debug" => false,
"newestOnTop" => false,
"progressBar" => true,
"positionClass" => "toast-bottom-right",
"preventDuplicates" => false,
"onclick" => null,
"showDuration" => "300",
"hideDuration" => "1000",
"timeOut" => "10000",
"extendedTimeOut" => "1000",
"showEasing" => "swing",
"hideEasing" => "linear",
"showMethod" => "fadeIn",
"hideMethod" => "fadeOut"
]
];
使用方法
使用Toastr facade Toastr::
或辅助函数toast()->
来访问本包中的方法。
Toastr::error(string $message, string $title = null, array $options = []);
toastr()->error(string $message, string $title = null, array $options = []);
示例
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use TJGazel\Toastr\Facades\Toastr;
class DashboardController extends Controller
{
public function index()
{
Toastr::info('welcome admin!');
return view('dashoard.index');
}
}
您还可以使用以下方法链接多个消息
toastr()
->error('Unauthorized!', 'Error 401')
->info('Authentication required to access dashboard page', null, ['timeOut' => 15000]);
注意,在第3个参数
中,您可以自定义toastr选项
。查看config/toastr.php
文件以获取所有可能选项。
所有方法
toastr()->info();
toastr()->warning();
toastr()->success();
toastr()->error();