aldaflux/youtube-utils-bundle
Installation
composer require aldaflux/youtube-utils-bundle
Ensure your config/bundles.php includes:
return [
// ...
Aldaflux\YoutubeUtilsBundle\AldafluxYoutubeUtilsBundle::class => ['all' => true],
];
Configuration
Add to config/packages/aldaflux_youtube_utils.yaml:
aldaflux_youtube_utils:
api_key: "%env(YOUTUBE_API_KEY)%" # Required
default_language: "en-US" # Optional
First Use Case: Fetch Video Info Inject the service in a controller:
use Aldaflux\YoutubeUtilsBundle\Service\YoutubeService;
class VideoController extends AbstractController
{
public function show(YoutubeService $youtube): Response
{
$video = $youtube->getVideoDetails('dQw4w9WgXcQ');
return $this->render('video/show.html.twig', ['video' => $video]);
}
}
Video Metadata Retrieval
$details = $youtube->getVideoDetails('VIDEO_ID');
// Returns: title, description, publishedAt, viewCount, etc.
Search Functionality
$results = $youtube->searchVideos('laravel tutorial', ['maxResults' => 5]);
// Returns: array of video/snippet data
Channel Management
$channel = $youtube->getChannelDetails('UC_x5XG1OV2P6uZZ5FSM9Ttw');
// Returns: channel stats, uploads, etc.
Twig Integration
{% for video in videos %}
<h3>{{ video.snippet.title }}</h3>
<iframe width="560" height="315"
src="https://www.youtube.com/embed/{{ video.id.videoId }}"
frameborder="0"></iframe>
{% endfor %}
Rate-Limited Calls Use dependency injection to wrap calls in a retry mechanism:
$youtube->withRetry(3, 1000)->getVideoDetails('VIDEO_ID');
Caching Responses Decorate the service to cache API responses:
$youtube->setCache(new SymfonyCacheAdapter());
Event-Driven Updates
Subscribe to youtube.utils.video.fetched events for post-processing:
$dispatcher->addListener('youtube.utils.video.fetched', function ($event) {
// Log, transform, or store data
});
API Key Restrictions
403 Forbidden errors in logs for missing scopes.Quota Limits
429 Too Many Requests.Deprecated Endpoints
videos.list(by=rating) (deprecated). Use commentThreads.list instead.Twig Template Caching
aldaflux_youtube_utils.yaml:
php bin/console cache:clear
Enable Verbose Logging
aldaflux_youtube_utils:
debug: true
Logs API calls to var/log/dev.log.
Validate Responses
Always check $youtube->getLastResponse() for errors:
if ($youtube->hasErrors()) {
throw new \RuntimeException($youtube->getErrorMessage());
}
Custom Response Mappers Override default response handling:
$youtube->setResponseMapper(new CustomVideoMapper());
Add New Endpoints
Extend YoutubeService and register as a decorator:
services:
app.youtube.decorated:
decorates: aldaflux.youtube.utils.service
arguments: ['@app.youtube.decorated.inner']
Webhook Integration
Use youtube.utils.webhook.received events to process real-time updates (e.g., new uploads).
Language Codes
Use RFC 5646 codes (e.g., en-US, es-MX). Avoid en alone—it defaults to en-US.
Pagination
Always check pageToken in responses for multi-page results:
do {
$results = $youtube->searchVideos('query', ['pageToken' => $pageToken]);
$pageToken = $results['nextPageToken'];
} while ($pageToken);
How can I help you explore Laravel packages today?