flavorly / hybridly-share
快速闪存和分享 Hybridly 变量,可在会话和进程上持久化。
v0.0.2
2024-01-13 22:13 UTC
Requires
- php: ^8.1
- hybridly/laravel: ^v0.6.1
- illuminate/contracts: ^10.0
- laravel/framework: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-09-29 20:20:29 UTC
README
快速将变量闪存和分享到 Hybridly,这些变量将在会话或缓存以及不同的请求之间持久化。
安装
您可以通过 composer 安装此包
composer require flavorly/hybridly-share
您可以使用以下命令发布配置文件
php artisan vendor:publish --tag="hybridly-share-config"
这是发布配置文件的内容
<?php return [ /* |-------------------------------------------------------------------------- | Driver Configuration |-------------------------------------------------------------------------- | | You can configure hybridly share to use session or cache as the driver. | when using the cache driver hybridly share will leverage your current | cache driver and attempt to save the temporary shared keys there. | A unique key is used to generate the unique key for each user | | Drivers: 'cache' or 'session' are supported. | Prefix Key : hybridly_container_ | Cache TTL : Time in seconds to store the keys in cache. */ 'prefix_key' => 'hybridly_container_', 'driver' => 'session', 'session-driver' => \Flavorly\HybridlyShare\Drivers\SessionDriver::class, 'cache-driver' => \Flavorly\HybridlyShare\Drivers\CacheDriver::class, 'cache-ttl' => 60, /* |-------------------------------------------------------------------------- | Persistent Keys |-------------------------------------------------------------------------- | | Here you may configure the keys that should be persisted on the session, | even if they are empty they will be mapped to their primitives configured here. | Might not be needed since hybridly provides a persist method. */ 'persistent-keys' => [ // 'some-key' => 'some-value', // 'messages => [], ], /* |-------------------------------------------------------------------------- | Ignore URLs & Params |-------------------------------------------------------------------------- | | The URls to ignore by default, because hybridly runs on web middleware | Default For URLS: ['broadcasting/auth',...] | */ 'ignore_urls' => [ 'nova-api*', 'filament-api*', 'broadcasting/auth', 'telescope*', 'horizon*', '_debugbar*', '_ignition*', ], ];
用法
您可以在代码的任何位置使用 Hybridly Share 助手,并直接将变量分享到 Hybridly。请注意,这些值仅保留在当前或下一个请求生命周期中,一旦分享到 Hybridly,它们将被清除。您还可以使用闭包,这些闭包在底层将被转换为 Laravel Closure Serializer(之前为 Opis)
// Or using the helper hybridly()->container()->share('foo', 'bar'); hybridly()->container()->share('foo', fn() => 'bar'); hybridly()->container()->append('fruits','bananas'); hybridly()->container()->append('fruits','potatoes'); hybridly()->container()->share('foo', ['bar' => 'foo', 'baz' => fn() => 'bar']); // On Controllers return back() return back()->hybridly('foo', 'bar'); return back()->hybridly('foo', fn() => 'bar'); // Conditional Sharing hybridly()->container()->shareIf($foo === true, 'foo', 'bar'); hybridly()->container()->shareUnless($foo === false, 'foo', 'bar'); // Sharing to a user // Only available if driver is cache, otherwise session will always use the current logged user hybridly()->container()->forUser($user)->append('foo', 'bar');
为什么选择 Hybridly Share?
此包旨在与 Hybridly 框架一起使用。Hybridly 提供了一种分享变量的好方法,但有时您可能希望数据在会话或缓存中持久化。
一些用例
- 在重定向之前分享数据(例如:back()->with('foo','bar') 可以通过 back()->hybridly('foo','bar') 来复制)
- 从将结束重定向或新生命周期的请求中分享数据
- 从服务直接分享数据
- 在响应请求/页面之前,从代码的任何位置分享数据
- 从命令/作业到特定用户分享数据
- 避免通过来回分享会话变量污染 Hybridly 中间件。
- 等等。
如果您正在寻找实时分享,这个包可能不是最佳选择,建议使用与 Pusher 或 Soketi 配对的 Laravel Echo。
测试
composer test
安全漏洞
请查看我们的安全策略,了解如何报告安全漏洞。
致谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅许可文件。