Installation
composer require dekalee/cdn77-bundle
Register the bundle in config/app.php (Laravel 5.5+) under providers:
Dekalee\Cdn77Bundle\DekaleeCdn77Bundle::class,
Configuration
Add to .env:
DEKALEE_CDN77_LOGIN=your_login
DEKALEE_CDN77_PASSWORD=your_password
Publish the config (if needed):
php artisan vendor:publish --provider="Dekalee\Cdn77Bundle\DekaleeCdn77Bundle" --tag="config"
Update config/cdn77.php (if published).
First Use Case
Inject the Cdn77Client into a service/controller:
use Dekalee\Cdn77Bundle\Client\Cdn77Client;
public function __construct(private Cdn77Client $cdn77) {}
// Purge a URL
$this->cdn77->purge(['url' => 'https://example.com/image.jpg']);
Purging Assets
$this->cdn77->purge(['url' => 'https://example.com/image.jpg']);
$urls = ['https://example.com/image1.jpg', 'https://example.com/image2.jpg'];
$this->cdn77->purge($urls);
$this->cdn77->purgeAll();
Uploading Files
Cdn77 library (not directly exposed by the bundle) via the client:
$this->cdn77->getClient()->uploadFile($filePath, $cdnPath);
Resource Management
$resources = $this->cdn77->listResources();
$this->cdn77->deleteResource(['url' => 'https://example.com/image.jpg']);
Event-Driven Purging
illuminate.filesystem.updated) to auto-purge:
Event::listen('illuminate.filesystem.updated', function ($event) {
$this->cdn77->purge(['url' => $event->path]);
});
Service Container Binding Bind the client to a custom interface for better testability:
$this->app->bind(
Dekalee\Cdn77Bundle\Contracts\Cdn77Interface::class,
Dekalee\Cdn77Bundle\Client\Cdn77Client::class
);
Queue Purging Offload purging to a queue job to avoid blocking requests:
class PurgeCdnJob implements ShouldQueue {
public function handle(Cdn77Client $cdn77) {
$cdn77->purge(['url' => $this->url]);
}
}
Middleware for Dynamic URLs Create middleware to purge CDN on route changes:
public function handle($request, Closure $next) {
$response = $next($request);
$this->cdn77->purge(['url' => $request->url()]);
return $response;
}
Authentication Failures
.env credentials match CDN77 account.try {
$this->cdn77->purge(['url' => 'test']);
} catch (\Exception $e) {
Log::error('CDN77 Error: ' . $e->getMessage());
}
Rate Limiting
use Symfony\Component\Process\Exception\ProcessTimedOutException;
try {
$this->cdn77->purge($urls);
} catch (ProcessTimedOutException $e) {
sleep(2); // Retry after delay
$this->cdn77->purge($urls);
}
URL Formatting
http:// or https://). Relative paths will fail.Bundle Compatibility
Enable API Logging
Add to config/cdn77.php:
debug: true,
Logs will appear in storage/logs/laravel.log.
Check API Responses Inspect raw responses via:
$response = $this->cdn77->getClient()->purge(['url' => 'test']);
Log::debug('CDN77 Response:', [$response->getBody()]);
Custom API Endpoints Override URLs in config:
dekalee_cdn77:
purge: https://custom-api.example.com/purge
Extend Client Create a decorator for additional logic:
class ExtendedCdn77Client implements Cdn77Interface {
private $cdn77;
public function __construct(Cdn77Client $cdn77) {
$this->cdn77 = $cdn77;
}
public function purge(array $urls) {
// Add analytics or pre-processing
$this->cdn77->purge($urls);
}
}
Add Custom Commands Extend the bundle with Artisan commands:
Artisan::command('cdn:purge-all', function () {
$this->cdn77->purgeAll();
});
Batch Purges Group URLs into chunks of 100 (CDN77’s recommended limit):
array_chunk($urls, 100, function ($chunk) {
$this->cdn77->purge($chunk);
});
Cache Purge Events Cache purge operations to avoid redundant calls:
if (!Cache::has("cdn_purge_{$url}")) {
$this->cdn77->purge(['url' => $url]);
Cache::put("cdn_purge_{$url}", true, now()->addHours(1));
}
How can I help you explore Laravel packages today?