amanank / hal-client
A PHP client for interacting with HAL APIs.
Installs: 1 321
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 5
Requires
- php: >=7.2
- guzzlehttp/guzzle: ^7.0
- illuminate/filesystem: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
- dev-main / 0.1.x-dev
- v0.1.9-alpha
- v0.1.8-alpha
- v0.1.7-alpha
- v0.1.6-alpha
- v0.1.5-alpha
- v0.1.4-alpha
- v0.1.3-alpha
- v0.1.2-alpha
- v0.1.1-alpha
- v0.1.0-alpha
- dev-20-excepton-response-code
- dev-22-add-tooptions-to-metalfixtimeperiodenum
- dev-18-relation-model-datatype-is-not-extended-model
- dev-15-rename-hal-model-search-function
- dev-14-getter-throws-error-on-enum-fields-for-new-entity
- dev-12-model-search-method-does-not-return-collection-of-entities
- dev-10-config-path-incorrect-in-service-provider
- dev-6-pacakage-auto-discovery
- dev-5-more-halhasmany-test
- dev-3-postrelationship-test
- dev-1-write-tests
This package is auto-updated.
Last update: 2025-04-25 08:29:52 UTC
README
A PHP client for interacting with HAL APIs.
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.
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
- Fork the repository.
- Create a new branch (
git checkout -b feature-branch
). - Make your changes.
- Commit your changes (
git commit -am 'Add new feature'
). - Push to the branch (
git push origin feature-branch
). - Create a new Pull Request.
Support
If you have any questions or need support, please open an issue on the GitHub repository.