承接 amanank/hal-client 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

amanank/hal-client

Composer 安装命令:

composer require amanank/hal-client

包简介

A PHP client for interacting with HAL APIs.

README 文档

README

A PHP client for interacting with HAL APIs. Tested with Laravel 8–12.

Installation

1. Install via Composer

You can install the package via Composer. Run the following command in your terminal:

composer require amanank/hal-client

If you haven't already, make sure to include the Composer autoload file in your project:

require 'vendor/autoload.php';

2. Publish the Configuration File

After installing the package, you can publish the configuration file using the following Artisan command:

php artisan vendor:publish --tag=config --provider="Amanank\HalClient\Providers\HalClientServiceProvider"

This will create a configuration file named hal-client.php in your config directory.

Note

Laravel's auto-discovery feature will automatically register the HalClientServiceProvider for you. You do not need to manually register it in your config/app.php file. For local development in this monorepo, the package is available via a path repository (packages/*/*) with symlinks enabled, so you can edit the package in place; in CI/prod, Composer will fall back to Packagist.

3. Configure hal-client.php

After registering the service provider, you need to configure it. Create a configuration file named hal-client.php in your config directory with the following content:

return [
    'base_uri' => env('HAL_API_BASE_URI', 'https://example.com/api/v1/'),
    'headers' => [
        'Authorization' => 'Bearer ' . env('HAL_API_TOKEN'),
        'Accept' => 'application/hal+json',
    ],
];

Make sure to set the HAL_API_BASE_URI and HAL_API_TOKEN environment variables in your .env file:

HAL_API_BASE_URI=https://api.example.com
HAL_API_TOKEN=your-api-token

4. Generate Models

To generate models from your HAL API, run the following commands in your terminal:

# Clear the application cache to ensure that any configuration changes are properly loaded
php artisan config:cache

# Generate models based on your HAL API schema
php artisan hal:generate-models

# Update the Composer autoloader to include the new models
composer dump-autoload

This will generate the necessary models based on your HAL API schema and ensure they are properly autoloaded.

Usage

Basic Usage

After generating the models, you can use them directly from the Amanank\HalClient\Models\Discovered namespace or extend them in your App\Models namespace.

Using Models Directly

use Amanank\HalClient\Models\Discovered\User;
use Amanank\HalClient\Models\Discovered\Post;
use Amanank\HalClient\Models\Discovered\Tag;
use Amanank\HalClient\Models\Discovered\Comment;

// Create a new user
$user = new User();
$user->username = 'john.doe';
$user->email = 'john.doe@example.com';
$user->save();

// Create a new post
$post = new Post();
$post->title = 'My First Post';
$post->content = 'This is the content of my first post.';
$post->user()->associate($user);
$post->save();

// Create a new tag
$tag = new Tag();
$tag->name = 'PHP';
$tag->save();

// Create a new comment
$comment = new Comment();
$comment->content = 'Great post!';
$comment->post()->associate($post);
$comment->user()->associate($user);
$comment->save();

Extending Models

You can also extend the generated models in your App\Models namespace:

namespace App\Models;

use Amanank\HalClient\Models\Discovered\User as DiscoveredUser;

class User extends DiscoveredUser {
    // Add your custom methods or properties here
}

Creating and Updating Models

use App\Models\User;
use App\Models\Post;
use App\Models\Tag;
use App\Models\Comment;

// Create a new user
$user = new User();
$user->username = 'john.doe';
$user->email = 'john.doe@example.com';
$user->save();

// Update a user
$user = User::find(1);
$user->email = 'new.email@example.com';
$user->save();

Searching Models

You can get models using the Model::get method, which accepts parameters $page = null, $size = null, and $sort = null, and returns a LengthAwarePaginator.

Additionally, any search methods exposed by the HAL API can be used directly. For example, User::findByLastName returns a collection, and User::getByEmail returns a User or null.

use Amanank\HalClient\Models\Discovered\User;

// Get paginated users
$users = User::get($page = 1, $size = 10, $sort = 'username');

// Find users by last name
$usersByLastName = User::findByLastName('Doe');

// Get user by email
$userByEmail = User::getByEmail('john.doe@example.com');

Working with Relationships

use App\Models\User;
use App\Models\Post;
use App\Models\Tag;
use App\Models\Comment;

// Get user's posts
$user = User::find(1);
$posts = $user->posts;

// Get post's comments
$post = Post::find(1);
$comments = $post->comments;

// Attach a tag to a post
$post = Post::find(1);
$tag = Tag::find(1);
$post->tags()->attach($tag);

// Detach a tag from a post
$post->tags()->detach($tag);

Working with Enums

Enums are located under the Amanank\HalClient\Models\Discovered\Enums namespace and are prefixed with the model name, such as UserStatusEnum and PostStatusEnum.

use Amanank\HalClient\Models\Discovered\Enums\UserStatusEnum;
use App\Models\User;

// Set user status
$user = User::find(1);
$user->status = UserStatusEnum::ACTIVE;
$user->save();

// Get users with a specific status
$activeUsers = User::where('status', UserStatusEnum::ACTIVE)->get();

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Make your changes.
  4. Commit your changes (git commit -am 'Add new feature').
  5. Push to the branch (git push origin feature-branch).
  6. Create a new Pull Request.

Support

If you have any questions or need support, please open an issue on the GitHub repository.

amanank/hal-client 适用场景与选型建议

amanank/hal-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.32k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 amanank/hal-client 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 14
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-11