Laravel 项目工具
dev-master
2024-06-07 08:40 UTC
Requires
- php: ^8.0
- ext-curl: *
- ext-zip: *
- laravel/framework: ^9.2|^10.0|^11.0
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-07 09:30:26 UTC
README
这是一个用于 Laravel 项目的工具集合。本包
要求
- PHP ^8.0
- Laravel ^9.2
安装
在您的 composer.json 中添加此存储库
"repositories": [ { "type": "vcs", "url": "https://github.com/labelgrupnetworks/laravel-utilities" } ]
安装 composer 依赖项后。
composer install
Composer 需要访问令牌来安装依赖项,如果您已有,请记下。
此包将不断更新。如果您想更新包以获得最新版本
composer update labelgrup/laravel-utilities
目录
命令
MakeApiRequest
创建一个适用于 API 请求的请求命令,在 /App/Http/Requests/Api/ 中。解析失败请求和响应使用 JSON。
此实体类似于原生 Laravel 请求
php artisan make:api-request {ApiRequestName}
MakeUseCase
创建一个在 /App/UseCases/ 中的 UseCase 命令,以解耦不同的 Strong 动作。
php artisan make:use-case {UseCaseName}
生成的类扩展到 UseCase 并具有 UseCaseInterface。此类需要实现 action 方法并具有响应的 handler 方法。
class ExampleUseCase extends \Labelgrup\LaravelUtilities\Core\UseCases\UseCase { public function action() { /* * Require implement use case action */ } public function handle(): \Labelgrup\LaravelUtilities\Core\UseCases\UseCaseResponse { /* * This method is optional to customize response. Require use UseCaseResponse class */ } }
如果您想自定义响应,实现 handle 和返回方法到 UseCaseResponse。
您可以指定 $response_message 为设置 OK 响应的消息。
public string $response_message = 'Action has been finished';
辅助函数
ApiResponse
此类帮助解析 API 的响应。方法
// Help to parse a success response public static function done( string $message, array $data = [], int $code = Response::HTTP_OK ): JsonResponse // Help to parse a fail response public static function fail( string $message, array $errors = [], int $code = Response::HTTP_OK ): JsonResponse // Help to parse a response public static function response( array $data, int $code ): JsonResponse // Help to parse a response public static function response( array $data, int $code ): JsonResponse
Image
此类简化了图像的一些操作。
// Get extension from Image url public static function getExtensionImageFromUrl( string $url ): ?string // Destroy a image public static function destroy( string $src ): bool // Download image from url public static function downloadFromUrl( string $url, string $fileName ): void
Password
此类简化了密码的一些操作。
// Get secure rules for passwords public static function rule( int $min_size = self::PASSWORD_LENGTH ): \Illuminate\Validation\Rules\Password // Generate a secure and unleaked password public static function generateSecurePassword( int $length = self::PASSWORD_LENGTH, int $max_retries = self::MAX_RETRIES ): string // Checks if the given password is already leaked public static function isLeakedPassword( string $password ): bool
Text
此类简化了文本的一些操作。
// Sanitize a text public static function sanitize( string $text, string $divider = '-' ): string
Time
此类简化了时间的一些操作。
// Parse time from seconds to humans public static function parseTimeForHumans( int $inputSeconds, string $unitMin = 's' ): string
Zip
此类简化了 zip 文件的一些操作。
// Create a zip file of a folder with hierarchy subfolders public static function create( string $zipFile, string $sourcePath ): ?string
规则
SlugRule
获取用于在 RequestForm 中验证 slugs 的规则