Three ways to emit a <link rel="canonical"> tag from a template. All three go through the same generator and produce identical output.

resources/views/components/layouts/app.blade.php
<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.

The emitted tag is a single, HTML-escaped line:

<link rel="canonical" href="https://example.com/products/item" />
  • <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:

app/Http/Controllers/ProductController.php
$url = canonical('/products/item'); // "https://example.com/products/item"
$generator = canonical(); // CanonicalUrlGeneratorInterface instance

The component’s markup lives in a package view. Publish it to customise the tag:

Terminal window
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:

resources/views/vendor/canonical/components/canonical.blade.php
<link rel="canonical" href="{{ $canonicalUrl() }}" />
<link rel="alternate" hreflang="en" href="{{ $canonicalUrl() }}" />