novaway/open-graph-bundle
Novaway OpenGraphBundle is a Symfony bundle for adding Open Graph metadata to pages. It provides configuration and Twig helpers to define and render social sharing tags like title, description, image, and type, helping improve link previews on social platforms.
composer require novaway/open-graph-bundle. Expect version friction: the last tagged release is 2019-10-22, so on Symfony 6/7 you will likely need --with-all-dependencies, a widened constraint, or a fork.config/bundles.php if Flex does not provide a recipe (it very likely does not):return [
// ...
Novaway\OpenGraphBundle\NovawayOpenGraphBundle::class => ['all' => true],
];
novaway/open-graph library into the container. Open these two files first — they are the source of truth, not the README:
vendor/novaway/open-graph-bundle/DependencyInjection/Configuration.php (actual config keys)vendor/novaway/open-graph-bundle/Resources/config/services.xml (actual service IDs and aliases)config/packages/novaway_open_graph.yaml — point it at a fetcher service (see Patterns). If the bundle requires a fetcher and none is set, the container will fail at compile time, so check the config tree first.OpenGraph (or its interface alias) and scrapes a URL:use Novaway\OpenGraph\OpenGraphInterface;
public function preview(string $url, OpenGraphInterface $openGraph): JsonResponse
{
$og = $openGraph->fetch($url); // method name: verify in vendor/novaway/open-graph/src
return $this->json([
'title' => $og->getTitle(),
'description' => $og->getDescription(),
'image' => $og->getImage(),
'url' => $og->getUrl(),
]);
}
Run bin/console debug:container novaway to confirm the exact service ID and whether autowiring is aliased before writing the first caller.
Custom fetcher (recommended). The default cURL fetcher is thin. Implement the library's FetcherInterface with Guzzle/Symfony HttpClient so you get timeouts, redirect limits, and retries, then register it and reference it in config:
final class GuzzleFetcher implements FetcherInterface
{
public function __construct(private HttpClientInterface $http) {}
public function fetch(string $url): string
{
return $this->http->request('GET', $url, [
'timeout' => 5,
'max_redirects' => 3,
'headers' => ['User-Agent' => 'MyApp/1.0 OpenGraph preview'],
])->getContent();
}
}
# config/services.yaml
App\OpenGraph\GuzzleFetcher: ~
# config/packages/novaway_open_graph.yaml
novaway_open_graph:
fetcher: App\OpenGraph\GuzzleFetcher
Caching decorator. Scraping is slow and remote; wrap the OpenGraph service with PSR-6 cache keyed by URL so repeated previews never leave the app:
final class CachedOpenGraph implements OpenGraphInterface
{
public function __construct(
private OpenGraphInterface $inner,
private CacheItemPoolInterface $cache,
) {}
// delegate fetch(), cache the returned value for e.g. 1 hour
}
Decorate instead of forking — the bundle exposes the interface, so services.yaml decoration works cleanly.
Async previews. For bulk imports (e.g. seeding a catalog from vendor URLs), dispatch a Messenger message per URL and do the fetch in a worker. A slow remote site should never block a form submit.
Form prefill pattern. The classic use case: a "paste product URL" field in an admin form, a small JSON endpoint that returns {title, image, description}, Alpine/Stimulus fills the fields, the user confirms and saves. Keep the fetch server-side to avoid CORS and to reuse your fetcher's SSRF guard.
Testing. In your app tests, mock OpenGraphInterface — never hit the network. For the library's own behavior, write a fake fetcher that returns fixture HTML containing <meta property="og:title" ...>.
bundles.php registration and a hand-written config file. debug:config novaway_open_graph is the fastest way to learn the real schema.http/https, resolve the host and reject private/loopback/link-local ranges, cap redirects, and enforce a size limit on the response body.og:title, use name= instead of property=, or ship wrong values. Always fall back to <title>, <meta name="description">, and Twitter card tags before showing "no preview".mb_convert_encoding($html, 'UTF-8', $detected) before parsing.bin/console debug:container --show-private novaway lists the services; debug:autowiring OpenGraph tells you whether the interface alias exists. If autowiring fails, inject the concrete service ID explicitly.OpenGraph interface, (3) overriding the parser class in config if the bundle exposes it — check Configuration.php for a parser/class key.fetch() vs parse() method differ across the library's history; always confirm against vendor/novaway/open-graph/src/.How can I help you explore Laravel packages today?