labelgrup/laravel-utilities
Composer 安装命令:
composer require labelgrup/laravel-utilities
包简介
Utilities to Laravel projects
README 文档
README
A collection of utilities to improve and streamline Laravel projects.
✅ Requirements
- PHP
^8.1 - Laravel
^10.43
📦 Installation
composer require labelgrup/laravel-utilities
📚 Table of Contents
⚙️ Commands
📝 MakeApiRequest
Creates a Request tailored for API usage in /App/Http/Requests/Api/. Handles request parsing and JSON-based responses/failures.
Behaves like native Laravel Form Requests.
php artisan make:api-request {ApiRequestName}
🧠 MakeUseCase
Generates a UseCase class in /App/UseCases/ to decouple and organize business logic into strong actions.
php artisan make:use-case {UseCaseName} {--without-validation}
The generated class extends UseCase and implements UseCaseInterface. You'll need to define the action() method and optionally validate() for input validation.
use Labelgrup\LaravelUtilities\Core\UseCases\UseCase; use Labelgrup\LaravelUtilities\Core\UseCases\WithValidateInterface; class ExampleUseCase extends UseCase implements WithValidateInterface { public function action() { // Implement your use case here } public function validate(): void { // Implement validation logic here } }
To customize the response, override the handle() method and return a UseCaseResponse object.
public string $response_message = 'Action has been finished'; public int $success_status_code = 201;
❗ CustomException
Provides a custom exception handler that extends Exception and implements Laravel’s Renderable interface for better control over API error responses.
public function __construct( public string $error_code, public string $error_message, public int $http_code = Response::HTTP_INTERNAL_SERVER_ERROR, public ?array $report_data = ['logs' => []], public ?array $response = [], public bool $should_render = true )
Properties
error_code: Identifier for the exception.error_message: Message returned in the response.http_code: HTTP status code.report_data: Additional info for logs.response: Custom response body.should_render: Whether the exception should be rendered or not.
🧰 Helpers
📤 ApiResponse
Simplifies API response formatting:
ApiResponse::done(string $message, array $data = [], int $code = 200, bool $streamJson = false); ApiResponse::fail(string $message, array $errors = [], int $code = 400, bool $streamJson = false); ApiResponse::response(array $data, int $code, bool $streamJson = false);
🧯 ExceptionHandler
Centralized error rendering.
public static function render(\Throwable $exception, Request $request);
Laravel 10.x
In app/Exceptions/Handler.php:
ExceptionHandler::render($e, $request);
Laravel 11.x
In bootstrap/app.php:
->withExceptions(function (Exceptions $exceptions) { ExceptionHandler::render($exceptions); })
🖼️ Image
Image utilities:
Image::getExtensionImageFromUrl(string $url): ?string; Image::destroy(string $src): bool; Image::downloadFromUrl(string $url, string $fileName): void;
🔐 Password
Password utilities:
Password::rule(int $min_size): PasswordRule; Password::generateSecurePassword(int $length = 12): string; Password::isLeakedPassword(string $password): bool;
✍️ Text
Text sanitization:
Text::sanitize(string $text, string $divider = '-'): string;
⏳ Time
Human-readable time conversion:
Time::parseTimeForHumans(int $inputSeconds, string $unitMin = 's'): string;
🗜️ Zip
Zipping utilities:
Zip::create(string $zipFile, string $sourcePath): ?string;
🧾 Rules
🐌 SlugRule
Get a rule for validating slugs in RequestForm.
统计信息
- 总下载量: 8.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-06-08