Installation Add the package via Composer:
composer require dayploy/daypia-bundle
Register the bundle in config/app.php under providers:
Dayploy\DaypiaBundle\DaypiaServiceProvider::class,
Publish Config Publish the default configuration:
php artisan vendor:publish --provider="Dayploy\DaypiaBundle\DaypiaServiceProvider" --tag="config"
This generates config/daypia.php. Review and customize as needed.
First Use Case: Basic API Integration
Inject the DaypiaClient into a service or controller:
use Dayploy\DaypiaBundle\Services\DaypiaClient;
class MyController extends Controller
{
protected $daypia;
public function __construct(DaypiaClient $daypia)
{
$this->daypia = $daypia;
}
public function fetchData()
{
$response = $this->daypia->get('/endpoint');
return response()->json($response);
}
}
Environment Configuration
Ensure your .env includes:
DAYPIA_API_KEY=your_api_key_here
DAYPIA_BASE_URL=https://api.daypia.example
API Requests with Error Handling Use the client for standard HTTP methods with automatic error handling:
try {
$data = $this->daypia->post('/resource', [
'param1' => 'value1',
'param2' => 'value2'
]);
} catch (\Dayploy\DaypiaBundle\Exceptions\DaypiaException $e) {
Log::error('Daypia API Error: ' . $e->getMessage());
return response()->json(['error' => 'Service unavailable'], 503);
}
Authentication Attach authentication tokens dynamically:
$this->daypia->withAuthToken('custom_token')->get('/secure-endpoint');
Middleware Integration Create middleware to inject the client or transform responses:
namespace App\Http\Middleware;
use Dayploy\DaypiaBundle\Services\DaypiaClient;
class DaypiaMiddleware
{
public function __construct(DaypiaClient $daypia)
{
$this->daypia = $daypia;
}
public function handle($request, Closure $next)
{
$request->merge(['daypia_data' => $this->daypia->get('/data')]);
return $next($request);
}
}
Event Listeners
Listen for Daypia-specific events (e.g., daypia.request.sent or daypia.response.received):
namespace App\Listeners;
use Dayploy\DaypiaBundle\Events\DaypiaResponseEvent;
class LogDaypiaResponse
{
public function handle(DaypiaResponseEvent $event)
{
Log::info('Daypia Response', ['data' => $event->getData()]);
}
}
Service Container Binding Extend or bind custom clients:
$this->app->bind('custom.daypia.client', function ($app) {
return new \Dayploy\DaypiaBundle\Services\CustomDaypiaClient(
$app->make('config')['daypia']
);
});
Laravel HTTP Client Use Laravel’s HTTP client for advanced features (e.g., retries, middleware):
$this->daypia->withHttpClient(function (\Illuminate\Http\Client\PendingRequest $request) {
return $request->withOptions(['timeout' => 30]);
});
Queue Jobs Offload API calls to queues:
Dispatch(new FetchDaypiaData($this->daypia))->onQueue('daypia');
Testing
Mock the DaypiaClient in tests:
$mock = Mockery::mock(DaypiaClient::class);
$mock->shouldReceive('get')->andReturn(['data' => 'test']);
$this->app->instance(DaypiaClient::class, $mock);
Caching Responses Cache API responses using Laravel’s cache:
$cachedData = Cache::remember('daypia_resource', now()->addHours(1), function () {
return $this->daypia->get('/resource');
});
Configuration Overrides
Avoid overriding config/daypia.php directly in production. Use environment variables or Laravel’s config caching:
'api_key' => env('DAYPIA_API_KEY', config('daypia.default_api_key')),
Rate Limiting Daypia may enforce rate limits. Implement exponential backoff:
use Symfony\Component\HttpClient\Retry\RetryStrategy;
$this->daypia->withRetryStrategy(
RetryStrategy::fromConfig([
'max_retries' => 3,
'delay' => 1000,
'multiplier' => 2,
'max_delay' => 10000,
])
);
Deprecated Methods Check for deprecated methods in the package (if any) and update your codebase proactively. Example:
// Deprecated (if applicable)
$this->daypia->oldMethod();
// Use instead
$this->daypia->newMethod();
SSL Verification Disable SSL verification only in development (never in production):
$this->daypia->withOptions(['verify_peer' => false]); // ⚠️ Avoid in prod
Event Dispatching
Ensure events are properly bound in EventServiceProvider:
protected $listen = [
\Dayploy\DaypiaBundle\Events\DaypiaResponseEvent::class => [
\App\Listeners\LogDaypiaResponse::class,
],
];
Enable Debug Mode Toggle debug mode in config:
'debug' => env('APP_DEBUG', false),
This may log additional request/response details.
Log Raw Responses Use Laravel’s logging to inspect raw responses:
$response = $this->daypia->get('/endpoint');
Log::debug('Daypia Raw Response', [
'status' => $response['status'],
'data' => $response['data'] ?? null,
'headers' => $response['headers'] ?? null,
]);
Check HTTP Status Codes Handle specific status codes explicitly:
$response = $this->daypia->get('/endpoint');
if ($response['status'] === 404) {
abort(404, 'Resource not found on Daypia');
}
Validate API Keys Ensure your API key is correct and hasn’t been revoked:
try {
$this->daypia->validateApiKey();
} catch (\Dayploy\DaypiaBundle\Exceptions\InvalidApiKeyException $e) {
// Handle invalid key (e.g., notify admin)
}
Custom Request Transformers Extend the client to transform requests/responses:
namespace App\Services;
use Dayploy\DaypiaBundle\Services\DaypiaClient;
class CustomDaypiaClient extends DaypiaClient
{
protected function transformRequest(array $data)
{
$data['custom_field'] = 'value';
return parent::transformRequest($data);
}
}
Add Middleware Attach middleware to the HTTP client:
$this->daypia->withMiddleware(function ($handler) {
return function ($request, $options) use ($handler) {
// Pre-process request
$response = $handler($request, $options);
// Post-process response
return $response;
};
});
Create Custom Commands Build Artisan commands for Daypia-specific tasks:
php artisan make:command FetchDaypiaData
Example:
namespace App\Console\Commands;
use Dayploy\DaypiaBundle\Services\DaypiaClient;
use Illuminate\Console\Command;
class FetchDaypiaData extends Command
{
protected $signature = 'daypia:fetch {endpoint}';
protected $description = 'Fetch data from Daypia API';
public function handle(DaypiaClient $daypia)
{
$data = $daypia->get($this->argument('endpoint'));
$this->info($data);
}
}
Override Default Config
Extend the config in config/daypia.php:
return [
'defaults' => [
How can I help you explore Laravel packages today?