alhames/apihelper
Laravel/PHP helper utilities for building API responses and common API tasks. Provides convenience methods for formatting output, handling errors, and streamlining controller/service code when creating REST-style endpoints.
## Getting Started
### Minimal Setup
1. **Installation**
Add the package via Composer (note: this is an **alpha** release, proceed with caution):
```bash
composer require alhames/apihelper@dev-main --prefer-dist
Publish the config (if available) with:
php artisan vendor:publish --provider="Alhames\ApiHelper\ApiHelperServiceProvider"
Basic Usage The package provides a facade for API interactions. Start with:
use Alhames\ApiHelper\Facades\ApiHelper;
$response = ApiHelper::get('https://api.example.com/data');
First Use Case: Simple API Request Fetch and decode JSON from an API endpoint (note: response handling may differ in alpha):
$data = ApiHelper::get('https://api.example.com/users')->toArray();
Standardized API Calls Wrap repetitive API logic in a service layer (alpha may lack optimizations):
class UserService {
public function fetchUsers() {
return ApiHelper::get('/api/users')->toArray();
}
}
Authentication Handling Attach headers via config (verify alpha compatibility):
// config/apihelper.php
'headers' => [
'Authorization' => 'Bearer ' . config('services.api.token'),
],
Error Handling Centralize API error responses (alpha may lack built-in methods):
try {
$response = ApiHelper::post('/api/orders', $data);
if ($response->failed()) { // Hypothetical alpha method
throw new \Exception($response->error());
}
} catch (\Exception $e) {
Log::error("API Error: " . $e->getMessage());
}
Pagination Handle paginated responses (alpha may lack helpers):
$users = ApiHelper::get('/api/users?page=1')->toArray();
$totalPages = $users['pagination']['total_pages'] ?? 1;
Http client for critical paths.$mock = Mockery::mock('overload:Alhames\ApiHelper\Facades\ApiHelper');
$mock->shouldReceive('get')->andReturn((object) ['data' => []]);
Alpha Instability
json() may not exist; use toArray() or raw responses.Deprecated/Archived Base
No Facade/Provider Guarantee
use Alhames\ApiHelper\ApiHelper;
$response = ApiHelper::get(...);
Response Handling Quirks
$data = json_decode($response->body, true);
ApiHelper::debug(true); // Hypothetical alpha method
$raw = $response->getBody(); // Alpha may lack Laravel-like methods
Custom Requests Extend the base class (alpha may lack documentation):
class CustomRequest extends \Alhames\ApiHelper\Request {
public function alphaMethod() { ... }
}
Middleware (If Supported) Add middleware cautiously (alpha may not support this):
ApiHelper::extend(function ($client) {
$client->addMiddleware(new CustomMiddleware());
});
Fallback to Laravel HTTP Alpha is unreliable; prefer:
$response = Http::get('https://api.example.com/data');
guzzlehttp/guzzle (direct)spatie/laravel-http-client (Laravel-integrated)filp/whoops (for debugging alpha quirks).
NO_UPDATE_NEEDED would **not** apply here due to the alpha release introducing potential breaking changes and instability. The assessment must reflect the risks and quirks of an alpha package.
How can I help you explore Laravel packages today?