aldaflux / youtube-utils-bundle
Symfony Bundle for youtube utils
Package info
github.com/AlDaFlux/youtube-utils
Type:symfony-bundle
pkg:composer/aldaflux/youtube-utils-bundle
Requires
- php: >=5.3.2
- doctrine/doctrine-bundle: *
- symfony/framework-bundle: >=3.0
- twig/twig: *
README
Symfony Bundle for YouTube API v3 integration and utilities.
Features
- 🎥 YouTube API Service (
ApiYoutubeService): Easily fetch information about YouTube videos, channels, and playlists. - 🔗 YouTube ID Extraction: Automatically extract 11-character video IDs from any YouTube URL format.
- 📝 Form Type
YoutubeUrlCodeType: Custom Symfony Form Type accepting full YouTube URLs and transforming them into 11-character video IDs. - 📊 Web Profiler Integration (DataCollector): View all YouTube API requests and potential errors directly inside the Symfony Web Debug Toolbar and Profiler.
Installation
Add the package via Composer:
composer require aldaflux/youtube-utils-bundle
Configuration
Create the configuration file config/packages/aldaflux_youtube_utils.yaml and specify your YouTube API key:
aldaflux_youtube_utils: youtube_api_key: '%env(YOUTUBE_API_KEY)%'
Add your API key to your .env or .env.local file:
YOUTUBE_API_KEY="your_youtube_v3_api_key"
Usage
1. Service ApiYoutubeService
Inject ApiYoutubeService into your controllers or services:
use Aldaflux\YoutubeUtilsBundle\Utils\ApiYoutubeService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class VideoController extends AbstractController { public function show(ApiYoutubeService $youtubeService, string $videoId) { // Get a video by its ID $video = $youtubeService->getVideo($videoId); // Get a channel by its ID $channel = $youtubeService->getChannel('UC...'); // Get a playlist by its ID $playlist = $youtubeService->getPlaylist('PL...'); // Extract YouTube ID from a full URL $id = $youtubeService->getYoutubeIdFromUrl('https://www.youtube.com/watch?v=dQw4w9WgXcQ'); // returns "dQw4w9WgXcQ" } }
2. Form Type YoutubeUrlCodeType
Use YoutubeUrlCodeType in your FormBuilders. Users can enter a full YouTube URL or just the video ID, and the DataTransformer will store only the 11-character video ID.
use Aldaflux\YoutubeUtilsBundle\Form\Type\YoutubeUrlCodeType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; class ArticleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('youtubeVideoId', YoutubeUrlCodeType::class, [ 'label' => 'YouTube Video (URL or ID)', 'required' => false, ]); } }
3. Web Debug Toolbar & Profiler
This bundle automatically registers a DataCollector in Symfony. In development environment (APP_ENV=dev), calls to the YouTube API are logged and displayed inside the Web Profiler panel.
License
GPL-3.0+