Installation Add the package via Composer:
composer require cristianocorrea/hazu
Publish the config file (if available) or check config/hazu.php for defaults.
Basic Usage
The package provides a Hazu facade or service container binding (hazu). Initialize it with a simple configuration:
use Hazu\Facades\Hazu;
$hazu = Hazu::make('your_api_key');
First Use Case: Fetching Data Use the package to fetch data from an API (assuming it’s a wrapper for a service like a weather API, payment gateway, etc.):
$response = $hazu->get('/endpoint');
$data = json_decode($response->getBody(), true);
API Integration
hazu is for a payment service:
$payment = $hazu->charge($amount, $cardDetails);
Service Container Binding Bind the package to the Laravel container for dependency injection:
$this->app->bind('hazu', function ($app) {
return Hazu::make(config('services.hazu.key'));
});
Middleware for API Requests Create middleware to attach the API key or other headers to requests:
public function handle($request, Closure $next) {
$request->headers->set('X-API-Key', config('services.hazu.key'));
return $next($request);
}
Laravel Service Providers Extend the package’s functionality in a service provider:
public function register() {
$this->app->singleton('hazu', function ($app) {
return new Hazu(config('services.hazu.key'));
});
}
Queue Jobs for Async Operations Offload long-running API calls to queues:
dispatch(new ProcessHazuPayment($amount, $cardDetails));
Archived Package
laravel/framework:^5.1 if the package targets older Laravel).No Documentation
src folder for method signatures or usage patterns.Error Handling
try {
$response = $hazu->get('/endpoint');
} catch (\Exception $e) {
Log::error("Hazu API error: " . $e->getMessage());
return response()->json(['error' => 'Service unavailable'], 503);
}
Configuration Quirks
config/hazu.php exists. Defaults may not be provided.config/services.php:
'hazu' => [
'key' => env('HAZU_API_KEY'),
'base_url' => env('HAZU_BASE_URL', 'https://api.example.com'),
],
Deprecated Laravel Features
Input::old() instead of $request->old()). Update calls to modern Laravel syntax.Fallback to Raw HTTP If the package fails, fall back to Laravel’s HTTP client:
$response = Http::withHeaders(['X-API-Key' => config('services.hazu.key')])
->get('https://api.example.com/endpoint');
Testing Mock the package in tests to avoid hitting external APIs:
$this->mock(Hazu::class)->shouldReceive('get')->andReturn(response()->json(['test' => true]));
Extending Functionality Create a decorator class to add features:
class ExtendedHazu {
protected $hazu;
public function __construct(Hazu $hazu) {
$this->hazu = $hazu;
}
public function customMethod() {
return $this->hazu->get('/custom-endpoint');
}
}
Logging Log API responses for debugging:
$response = $hazu->get('/endpoint');
Log::debug('Hazu response', ['data' => $response->getBody()]);
Fork and Maintain If the package is critical, fork it on GitHub and update it for modern Laravel. Contribute fixes upstream if possible.
How can I help you explore Laravel packages today?