Installation:
composer require ee/embedly-bundle:dev-master
Add the bundle to config/bundles.php:
EE\Bundle\EmbedlyBundle\EEEmbedlyBundle::class => ['all' => true],
Configuration:
Add Embedly API key to .env:
EMBEDLY_API_KEY=your_api_key_here
Publish the default config (optional):
php bin/console config:dump-reference EEEmbedlyBundle
First Use Case: Fetch a URL's embed data in a controller:
use Symfony\Component\HttpFoundation\Response;
class EmbedController extends AbstractController
{
public function embed(Embedly $embedly): Response
{
$url = 'https://example.com';
$embed = $embedly->embed($url);
return $this->json($embed);
}
}
Service Injection:
Inject Embedly service directly into controllers, services, or twig extensions:
public function __construct(private Embedly $embedly) {}
Twig Integration: Pass embed data to templates:
{% for media in embedly.embed(url).media %}
<img src="{{ media.url }}" alt="{{ media.title }}">
{% endfor %}
Batch Processing: Fetch multiple URLs efficiently:
$urls = ['url1', 'url2', 'url3'];
$results = $embedly->batch($urls);
Custom Endpoints: Use specific Embedly endpoints (e.g., oembed, images):
$oembed = $embedly->oembed('https://youtube.com/watch?v=abc123');
$cacheKey = 'embedly_' . md5($url);
if (!$embed = $cache->get($cacheKey)) {
$embed = $embedly->embed($url);
$cache->set($cacheKey, $embed, 3600); // Cache for 1 hour
}
try {
$embed = $embedly->embed($invalidUrl);
} catch (\Embed\Exceptions\EmbedException $e) {
$this->addFlash('error', 'Embed failed: ' . $e->getMessage());
}
$message = new GenerateEmbedMessage($url);
$this->messageBus->dispatch($message);
API Key Management:
.env.Rate Limits:
$embedly->setRetryOptions(['max_retries' => 3, 'delay' => 1000]);
URL Validation:
$url = filter_var($rawUrl, FILTER_VALIDATE_URL);
if (!$url) throw new \InvalidArgumentException('Invalid URL');
Bundle Maturity:
dev-master).$embedly->setDebug(true); // Logs API requests/responses
EmbedException: Invalid API key or URL. Verify .env and input.ConnectionException: Network issues. Check proxy/firewall settings.Custom Responses: Override default response handling by extending the service:
class CustomEmbedly extends Embedly
{
public function embed($url)
{
$response = parent::embed($url);
return $this->transformResponse($response);
}
}
Register as a service in config/services.yaml:
services:
App\Service\CustomEmbedly:
decorates: 'embedly'
arguments: ['@embedly.inner']
Event Listeners: Hook into the bundle’s lifecycle (if exposed) to log or modify requests/responses.
Configuration Overrides:
Extend the default config in config/packages/ee_embedly.yaml:
ee_embedly:
api_key: '%env(EMBEDLY_API_KEY)%'
default_endpoint: 'oembed' # Override default 'embed'
How can I help you explore Laravel packages today?