longman / laravel-lodash
为 Laravel >=8.0 添加更多功能
Requires
- php: ^8.2
- laravel/framework: ^11.0
Requires (Dev)
- aws/aws-sdk-php: ^3.306
- elasticsearch/elasticsearch: ^8.13
- jetbrains/phpstorm-attributes: ^1.0
- laravel/horizon: ^5.24
- laravel/passport: ^12.0
- longman/php-code-style: ^10.1
- mockery/mockery: ^1.3
- neitanod/forceutf8: ^2.0
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^11.0
Suggests
- ext-igbinary: Needed for speed up Redis/Memcached data serialization and reduce serialized data size
- ext-json: Needed for using resources
- ext-lz4: Needed for serialization Redis traffic
- ext-lzf: Needed for serialization Redis traffic
- ext-msgpack: Needed for speed up Redis/Memcached data serialization and reduce serialized data size
- ext-redis: Needed for using phpredis
- aws/aws-sdk-php: Use AWS SQS >=3
- barryvdh/laravel-debugbar: Allow print compiled queries via function get_db_queries()
- beyondcode/laravel-self-diagnosis: Run system diagnosis with many custom checks
- elasticsearch/elasticsearch: Use Elasticsearch service in the laravel
- longman/laravel-multilang: Adds multilanguage functional to Laravel >=5.5
- neitanod/forceutf8: Allow encoding/decoding utf-8 strings
- ramsey/uuid: Use UUID identifiers in the eloquent models
- dev-master
- 12.0.0
- 11.0.2
- 11.0.1
- 11.0.0
- 10.3.0
- 10.2.1
- 10.2.0
- 10.1.0
- 10.0.1
- 10.0.0
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 8.0.1
- 8.0.0
- 7.1.3
- 7.1.2
- 7.1.1
- 7.1.0
- 7.0.0
- 6.0.1
- 6.0.0
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.6.3
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.1
- 4.4.0
- 4.3.0
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.13
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.16.1
- 0.16.0
- 0.15.1
- 0.15.0
- 0.14.0
- 0.12.0
- 0.11.0
- 0.10.1
- 0.10.0
- 0.9.1
- 0.9.0
- 0.8.4
- 0.8.3
- 0.8.2
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.3
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- 0.0.6
- 0.0.5
- 0.0.4
- 0.0.3
- 0.0.2
- 0.0.1
- dev-dependabot/composer/aws/aws-sdk-php-3.288.1
- dev-dev
This package is auto-updated.
Last update: 2024-09-06 10:25:49 UTC
README
此包为 Laravel >=8.0 项目添加了大量有用的功能
注意:对于低于 5.8 的 Laravel,请使用包版本 1.*,对于低于 8.0 的版本,请使用版本 4.*。
目录
安装
通过 Composer 安装此包。
在命令行中运行命令
composer require longman/laravel-lodash
将 LodashServiceProvider
添加到 app.php
中的服务提供者列表
'providers' => [ . . . /* * Package Service Providers... */ Longman\LaravelLodash\ServiceProvider::class, . . . ],
使用发布命令将包配置和翻译复制到您的应用程序
php artisan vendor:publish --provider="Longman\LaravelLodash\LodashServiceProvider"
使用
通用
根据访问者 IP 地址启用调试模式
将 Longman\LaravelLodash\Debug\DebugServiceProvider::class
添加到 config/app.php
,并在 config/lodash.php
配置文件中指定调试 IP
. . . 'debug' => [ 'ips' => [ // IP list for enabling debug mode //'127.0.0.1', ], ], . . .
将 created_by、updated_by 和 deleted_by 添加到 Eloquent 模型
有时我们需要知道谁创建了、更新了或删除了数据库中的条目。
为此,只需将 Longman\LaravelLodash\Eloquent\UserIdentities
特性添加到您的模型中,并更新迁移文件添加必要的列
$table->unsignedInteger('created_by')->nullable(); $table->unsignedInteger('updated_by')->nullable(); $table->unsignedInteger('deleted_by')->nullable();
在 Eloquent 模型中使用 UUID
为此,只需将 Longman\LaravelLodash\Eloquent\UuidAsPrimary
特性添加到您的模型中,并更新相关的迁移文件
$table->uuid('id')->primary();
此外,还可以通过在模型类中定义 uuidVersion
属性来指定 uuid 版本。
通过子查询或联合查询预先加载有限的多对多关系
通过子查询或联合查询使用限制预先加载多对多关系。要使用此功能,请将 Longman\LaravelLodash\Eloquent\ManyToManyPreload
特性添加到模型中。然后您可以使用 limitPerGroupViaUnion()
和 limitPerGroupViaSubQuery()
方法。例如,您想选择用户及其每个用户的 3 张相关用户照片。
$items = (new User)->with([ 'photos' => function (BelongsToMany $builder) { // Select via union. There you should pass pivot table fields array $builder->limitPerGroupViaUnion(3, ['user_id', 'photo_id']); // or // Select via subquery $builder->limitPerGroupViaSubQuery(3); }, 'other.relation1', 'other.relation2' ]); $items = $items->get();
现在每个用户模型都通过一个查询选择了 3 张照片模型。您可以在调用分组方法之前指定额外的 where 子句或 order by 字段。
使用 igbinary 的 Redis
Igbinary 是标准 php 序列化程序的替代品。Igbinary 以紧凑的二进制形式存储 php 数据结构。当使用 Redis 或类似基于内存的存储用于序列化数据时,可以显著节省空间。通过 Igbinary,重复的字符串只存储一次。Eloquent 对象集合从中受益匪浅。
默认情况下,Laravel 不提供启用 igbinary 序列化程序选项的功能,您必须使用 LaravelLodash 实现来做到这一点。
首先,确保您已通过以下指南启用了 PhpRedis 驱动程序 https://laravel.net.cn/docs/5.5/redis#phpredis
之后,在您的 App 提供者之前包含 Cache 和 Redis 服务提供者到 app.php
. . . Longman\LaravelLodash\Cache\CacheServiceProvider::class, Longman\LaravelLodash\Redis\RedisServiceProvider::class, . . .
您可以从配置中删除 Laravel 的 Cache 和 Redis 服务提供者,因为 LaravelLodash 提供者是从它们扩展的,因此实现了整个功能。
现在您可以在 config
文件夹下的 database.php
中指定序列化程序
此外,您还可以指定其他选项,如scan
等。请参阅https://github.com/phpredis/phpredis#setoption
Redis 客户端分片
PhpRedis扩展程序与原生Redis集群一起,也支持客户端分片。当您想在多个服务器之间分配数据,但不想使用原生Redis集群时,此功能非常有用。
默认情况下,Laravel未实现此功能。我们尝试修复了这个问题 😄
配置示例
. . . 'redis' => [ 'client' => 'phpredis', 'clusters' => [ 'options' => [ 'lazy_connect' => true, 'connect_timeout' => 1, 'read_timeout' => 3, 'password' => env('REDIS_PASSWORD', null), 'database' => env('REDIS_DATABASE', 0), 'prefix' => env('REDIS_PREFIX'), 'serializer' => Redis::SERIALIZER_IGBINARY, 'compression' => Redis::COMPRESSION_ZSTD, 'compression_level' => Redis::COMPRESSION_ZSTD_DEFAULT, ], 'default' => [ [ 'host' => env('REDIS_SHARD1_HOST', '127.0.0.1'), 'port' => env('REDIS_SHARD1_PORT', 6379), ], [ 'host' => env('REDIS_SHARD2_HOST', '127.0.0.2'), 'port' => env('REDIS_SHARD2_PORT', 6379), ], . . . ], ], ], . . .
AWS SQS Fifo 队列
Laravel默认不支持AWS FIFO队列,此包修复了这个问题。
您需要在app.php
文件中在您的App提供者之前添加QueueServiceProvider
服务提供者。
. . . Longman\LaravelLodash\Queue\QueueServiceProvider::class, . . .
由于LaravelLodash提供者从其中扩展并实现完整功能,因此您可以移除Laravel的Queue服务提供者从配置中。
现在您可以在config
文件夹下的queue.php
中添加新的连接
. . . 'sqs_fifo' => [ 'driver' => 'sqs.fifo', 'version' => 'latest', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'prefix' => env('AWS_SQS_URL'), 'queue' => env('AWS_SQS_DEFAULT_QUEUE'), 'region' => env('AWS_DEFAULT_REGION'), 'options' => [ 'type' => 'fifo', // fifo, normal 'polling' => 'long', // long, short 'wait_time' => 20, ], ], . . .
Elasticsearch 集成
首先,您必须安装官方的elasticsearch php sdk
composer require elasticsearch/elasticsearch
在app.php
中添加ElasticsearchServiceProvider
服务提供者之前,您的App提供者
. . . Longman\LaravelLodash\Elasticsearch\ElasticsearchServiceProvider::class, . . .
现在您可以在config
文件夹下的services.php
中添加配置
. . . 'elasticsearch' => [ 'enabled' => env('ELASTICSEARCH_ENABLED', false), 'log_channel' => ['daily'], 'hosts' => [ [ 'host' => env('ELASTICSEARCH_HOST', 'localhost'), 'port' => env('ELASTICSEARCH_PORT', 9200), ], ], 'connectionParams' => [ 'client' => [ 'timeout' => env('ELASTICSEARCH_TIMEOUT', 3), 'connect_timeout' => env('ELASTICSEARCH_TIMEOUT', 3), ], ], ], . . .
您可以通过Elasticsearch集成使用
$elasticsearch_manager = app(ElasticsearchManagerContract::class); // Call wrapped methods $elasticsearch_manager->createIndex('some-index'); // Or get native client and access their methods $client = $elasticsearch_manager->getClient(); $client->indices()->create($params);
您还可以通过可搜索的查询对象进行搜索。只需创建一个类并实现ElasticsearchQueryContract
,然后您可以将对象传递给performSearch
方法
$elasticsearch_manager = app(ElasticsearchManagerContract::class); $results = $elasticsearch_manager->performSearch($query);
检查已安装的包是否与 composer.lock 同步
为了开发目的,建议检查vendor文件夹是否与composer.lock文件同步。
为此,在composer.json中您必须添加脚本ComposerScripts::createPackageHash
. . . "post-autoload-dump": [ "Longman\\LaravelLodash\\Composer\\ComposerScripts::createPackageHash", . . . ], . . .
然后在AppServiceProvider::boot
中添加以下行
. . . if (config('app.debug')) { $checker = new ComposerChecker(base_path()); $checker->checkHash(); } . . .
辅助函数
扩展类
为了这个功能,您应该在config/app.php
文件中添加Longman\LaravelLodash\LodashServiceProvider::class
通过Laravel的内置宏功能扩展了类
请求类
Artisan 命令
为了这个功能,您应该在config/app.php
文件中添加Longman\LaravelLodash\LodashServiceProvider::class
中间件列表
XssSecurity
设置XSS安全头。可以在config/lodash.php
中配置排除URI等。
SimpleBasicAuth
向路由添加简单的基本认证。
在config/auth.php
中您必须添加
. . . 'simple' => [ 'enabled' => env('SIMPLE_AUTH_ENABLED', true), 'user' => env('SIMPLE_AUTH_USER', 'user'), 'password' => env('SIMPLE_AUTH_PASS', 'secret'), ], . . .
Blade 指令
为了这个功能,您应该在config/app.php
文件中添加Longman\LaravelLodash\LodashServiceProvider::class
杂项
自我诊断检查
要使用这些检查,您必须安装包:laravel-self-diagnosis
可用磁盘空间检查
... \Longman\LaravelLodash\SelfDiagnosis\Checks\AvailableDiskSpace::class => [ 'paths' => [ '/' => '100G', // At least 100G should be available for the path "/" '/var/www' => '5G', ], ], ...
文件系统磁盘可用
... \Longman\LaravelLodash\SelfDiagnosis\Checks\FilesystemsAreAvailable::class => [ 'disks' => [ 'local', 's3', ], ], ...
Elasticsearch健康检查
... \Longman\LaravelLodash\SelfDiagnosis\Checks\ElasticsearchCanBeAccessed::class => [ 'client' => ElasticSearchClient::class, ], ...
Php Ini选项检查
... \Longman\LaravelLodash\SelfDiagnosis\Checks\PhpIniOptions::class => [ 'options' => [ 'upload_max_filesize' => '>=128M', 'post_max_size' => '>=128M', 'memory_limit' => '>=128M', 'max_input_vars' => '>=10000', 'file_uploads' => '1', 'disable_functions' => '', ], ], ...
Php Ini选项检查
... \Longman\LaravelLodash\SelfDiagnosis\Checks\RedisCanBeAccessed::class => [ 'default_connection' => true, 'connections' => ['sessions'], ], ...
服务器可ping检查
... \Longman\LaravelLodash\SelfDiagnosis\Checks\ServersArePingable::class => [ 'servers' => [ [ 'host' => config('app.url'), 'port' => 80, 'timeout' => 1, ], [ 'host' => 'sqs.eu-west-1.amazonaws.com', 'port' => 443, 'timeout' => 3, ], [ 'host' => 'www.googleapis.com', 'port' => 443, 'timeout' => 3, ], ], ], ...
Horizon正在运行
... \Longman\LaravelLodash\SelfDiagnosis\Checks\HorizonIsRunning::class, ...
待办事项
编写更多测试并添加更多功能
故障排除
如果您喜欢走在前沿,请向laravel-lodash issues页面报告您找到的任何错误。
贡献
欢迎Pull requests。有关信息,请参阅CONTRIBUTING.md
许可证
请参阅此存储库中包含的LICENSE,以获取完整的MIT许可证副本,该项目据此许可。
致谢
完整的贡献者列表在Contributors