Three ways to emit a <link rel="canonical"> tag from a template. All three go
through the same generator and produce identical output.
Emitting the tag
Section titled “Emitting the tag”<head> <x-canonical /> {{-- current request --}} <x-canonical path="/products/item" /> {{-- a specific path --}} <x-canonical :path="$post->canonical_path" /> {{-- a dynamic path --}}</head>An empty string or / for path falls back to the bare canonical domain.
<head> @canonical {{-- current request --}} @canonical('/products/item') {{-- a string literal --}} @canonical($canonicalPath) {{-- a variable --}}</head>The directive escapes the URL with e() before printing, so query strings are
safe inside the attribute.
<head> <link rel="canonical" href="{{ canonical('/products/item') }}" /> <link rel="canonical" href="{{ canonical()->generate() }}" /> <meta property="og:url" content="{{ canonical()->generate() }}" /></head>You write the tag yourself, so the same URL can appear in more than one place.
The emitted tag is a single, HTML-escaped line:
<link rel="canonical" href="https://example.com/products/item" />Which one to use
Section titled “Which one to use”<x-canonical>— the clearest default, and the only form that takes a bound attribute (:path="$…").@canonical— the same tag with the least markup, for a literal or a plain variable.canonical()— when you want the URL string rather than a<link>tag.
The helper is not Blade-specific — it works anywhere in the application:
$url = canonical('/products/item'); // "https://example.com/products/item"$generator = canonical(); // CanonicalUrlGeneratorInterface instancePublishing the view
Section titled “Publishing the view”The component’s markup lives in a package view. Publish it to customise the tag:
php artisan vendor:publish --tag="canonical-views"Directoryresources/views/vendor/canonical/
Directorycomponents/
- canonical.blade.php your copy wins over the packaged one
The view receives a $canonicalUrl callable, so a customised tag keeps the same
generation logic:
<link rel="canonical" href="{{ $canonicalUrl() }}" /><link rel="alternate" hreflang="en" href="{{ $canonicalUrl() }}" />