定制 mzur/kirby-flash 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mzur/kirby-flash

Composer 安装命令:

composer require mzur/kirby-flash

包简介

Stores data in the session for the next request. Data is removed after the next page load.

README 文档

README

Tests

This is a fork of jevets\kirby-flash.

Allows you to "flash" data to the session, which will be available via the session on the next page load, after which the data is removed from the session.

Very useful for:

  • Saving submitted form data for form validation, specifically for the Post/Redirect/Get design pattern
  • Displaying Success or Error messages after a page reload

Quick Example

flash('thanks_message', 'Thank you for contacting us!');

Elsewhere...

<?php if (flash('thanks_message')): ?>
    <?php echo flash('thanks_message') ?>
<?php endif ?>

Installation

Install with composer:

# Kirby v2
composer require mzur/kirby-flash:^1.0
# Kirby v3
composer require mzur/kirby-flash:^2.0

Usage

Set data

flash('key', 'value');

flash('messages.success', ['Thanks for your feedback!']);
flash('messages.errors', ['Email is a required field']);
flash('username', 'jimihendrix');

Get data

$value = flash('key');

$success_messages = flash('messages.success'); // Array( 0 => 'Thanks for your feedback!' )
$username = flash('username'); // "jimihendrix"

Examples

flash('messages.errors', [
    'Email is required',
    'Password is required',
]);

flash('messages.errors'); // Array( 0 => 'Email is required', 1 => 'Password is required' )
<?php if (count(flash('messages.errors')) > 0): ?>
<div class="alert alert-error">
    <?php foreach (flash('messages.errors') as $message): ?>
        <div><?= html($message) ?></div>
    <?php endforeach ?>
</div>
<?php endif ?>

flash() Helper Function

This class loads a global helper function: flash($key, $value = '').

The flash() function is only defined if it doesn't already exist, so you could define your own flash() function if necessary. Most of the time, you'll probably just use flash() in your application.

When called with one parameter, the value is returned. If the key doesn't exist, then you'll get back an empty string.

flash('my_key');

When called with two parameters, the $value is set for the $key.

If $key already exists, $value will replace the existing $key's value.

flash('my_other_key', 'Some Value');
flash('my_other_key', 'Some Other Value');

flash('my_other_key'); // "Some Other Value"

You may store any kind of data you want in the session. As another example, you could store multiple form validation error messages as an array in a single key.

flash('messages.errors', ['Email is required.', 'Phone is required.']);

flash('messages.errors'); // Array( 0 => 'Email is required.', 1 => 'Phone is required.' )

Flash for current page load only

Sometimes it can be useful to flash a message for the current page load, not the next one. This use case arises when a message should be shown in a response to the same request, rather than a redirect.

The flash() helper method can be called with an optional third parameter, a boolean to toggle whether to keep the flashed variable only for the current request. The default value of this parameter is false which will keep the flashed variable for the next request.

flash('message', 'Message for redirect'); // Keep for next request
flash('message', 'Message for this response', true); // Keep only for current request

## Session Key

By default Flash stores data under the session key `_flash`.

So you *could* access flash data like `$kirby->session()->get('_flash')` if you wanted to.

### Changing the Session Key

Use the static method to change the flash key. (You should probably do this early on in your app, probably in `index.php` or `site.php`.)

```php
Jevets\Kirby\Flash::setSessionKey('_my_custom_key');

Getting the Session Key

Jevets\Kirby\Flash::sessionKey();

Contributing

Feel free to send a pull request!

Issues/Bugs

Please use the GitHub issue tracker.

mzur/kirby-flash 适用场景与选型建议

mzur/kirby-flash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 79.04k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「session」 「flash」 「kirby」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mzur/kirby-flash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mzur/kirby-flash 我们能提供哪些服务?
定制开发 / 二次开发

基于 mzur/kirby-flash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 79.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 29
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-26