wp-kit/flash

一个处理前端和后台闪存通知的 wp-kit 组件

2.0.1 2020-01-31 23:41 UTC

This package is not auto-updated.

Last update: 2024-09-21 21:19:47 UTC


README

这是一个处理前后端闪存通知的 wp-kit 组件。

该组件是为了在 Illuminate\Container\Container 中运行而构建的,非常适合 ThemosisAsselywp-kit/theme 等框架。

通常,Wordpress 开发者希望能够使用一个组件来处理存储在会话中的闪存并将其输出到客户端,通常是在重定向之后。

在 Wordpress 中,我们确实可以通过一些钩子伪造管理通知,但需要跳过几个障碍,你不得不写相当多的代码来处理会话存储和输出,并且目前没有前端闪存的钩子。

安装

如果你使用 Themosis,请通过在 Themosis 路由文件夹中通过 Composer 安装,否则在由 Composer 驱动的主题文件夹中安装

composer require "wp-kit/flash"

设置

添加服务提供者

只需在您的提供者配置中注册服务提供者和外观即可

//inside theme/resources/config/providers.config.php

return [
    //
    WPKit\Config\ConfigServiceProvider::class, // we need this too,
    Illuminate\Filesystem\FilesystemServiceProvider::class, // specify the driver provider
    Illuminate\Session\SessionServiceProvider::class, // we need this too
    WPKit\Flash\FlashServiceProvider::class
];

添加外观

如果你使用 Themosis 或其他 Iluminate 驱动的框架,你可能想添加 Facades,只需将它们添加到你的别名中即可

//inside theme/resource/config/theme.config.php

'aliases' => [
    //
    'AdminFlash' => WPKit\Flash\Facades\AdminFlash::class,
    'FrontendFlash' => WPKit\Flash\Facades\FrontendFlash::class
    //
]

添加配置和视图文件

尽管 wp-kit/flash 不需要配置文件,但我们确实需要发布视图文件,并且需要配置来设置 SessionProvider

wp-kit 组件安装配置文件的建议方法是通过 wp kit vendor:publish 命令。

首先,安装 WP CLI,然后安装此组件,wp kit vendor:publish 将会自动与 wp-kit/utils 一起安装,安装完成后,你可以运行

wp kit vendor:publish

有关更多信息,请访问 wp-kit/utils

或者,你可以手动将 配置文件视图文件 放到你的 theme/resources/configtheme/resources/views 目录中。

使用方法

注意: AdminFlash 会自动使用钩子 admin_notices 在管理区域输出通知。

重要: 不要忘记使用 Illuminate\Session\Middleware\StartSession 中间件来确保闪存持久。

use Themosis\Facades\Route;
use Illuminate\Session\Middleware\StartSession;

Route::group([
    'namespace' => 'Theme\Controllers',
    'middleware' => [
	StartSession::class, 
	'auth.form'
    ]
], function () {
    require themosis_path('theme.resources').'routes.php';
});	

使用外观

// Just in case you need to include the Facade in a custom namespace

use WPKit\Flash\Facades\AdminFlash;
use WPKit\Flash\Facades\FrontendFlash;

// Frontend

FrontendFlash::success('Well done!');
FrontendFlash::warning('Hmm, not sure about that...');
FrontendFlash::error('What on earth are you doing?');

$messages = FrontendFlash::all();

$html = FrontendFlash::render();

$chained = FrontendFlash::success('Well done!')->render();

FrontendFlash::clear();

FrontendFlash::display([
	'message' => 'Ooh, living dangerously are we?'
	'class' => 'some-classname'
]);

// Admin

AdminFlash::success('Well done!');
AdminFlash::warning('Hmm, not sure about that...');
AdminFlash::error('What on earth are you doing?');

$messages = AdminFlash::all();

$html = AdminFlash::render();

$chained = AdminFlash::success('Well done!')->render();

AdminFlash::clear();

AdminFlash::display([
	'message' => 'Ooh, living dangerously are we?'
	'class' => 'some-classname'
]);

使用辅助函数

// Frontend

flash('frontend')->success('Well done!');
flash('frontend')->warning('Hmm, not sure about that...');
flash('frontend')->error('What on earth are you doing?');

$messages = flash('frontend')->all();

$html = flash('frontend')->render();

$chained = flash('frontend')->success('Well done!')->render();

flash('frontend')->clear();

flash('frontend')->display([
	'message' => 'Ooh, living dangerously are we?'
	'class' => 'some-classname'
]);

// Admin

flash('admin')->success('Well done!');
flash('admin')->warning('Hmm, not sure about that...');
flash('admin')->error('What on earth are you doing?');

$messages = flash('admin')->all();

$html = flash('admin')->render();

$chained = flash('admin')->success('Well done!')->render();

flash('admin')->clear();

flash('admin')->display([
	'message' => 'Ooh, living dangerously are we?'
	'class' => 'some-classname'
]);

遍历消息

这只是一个指导,说明如何在需要输出每个闪存周围的标记的情况下,使用 wp-kit/flash 遍历大量闪存。

// within theme/resources/views/some-view.php

<div class="row">

	<?php foreach( flash('frontend')->all() as $message ) : ?>
	
		<div class="column">
		
			<?php flash( 'frontend' )->display( $message ); ?>
			
		</div>
		
	<?php endforeach; ?>
	
</div>

参与进来

要了解更多关于如何使用 wp-kit 的信息,请查看文档

查看文档

任何帮助都将受到欢迎。该项目是开源的,我们鼓励你参与。你可以通过以下方式为项目做出贡献:

  • 报告一个错误问题
  • 提出功能建议
  • 提交包含代码修复或功能的拉取请求
  • GitHub 上跟踪项目
  • 在您的社区中分享项目

有关为框架贡献的详细信息,请参阅贡献指南

要求

Wordpress 4+

PHP 5.6+

许可证

wp-kit/flash 是开源软件,采用 MIT 许可证。