Every entry point routes through CanonicalUrlGeneratorInterface, resolved from the container. The facade, helper, component, and directive are thin wrappers over its single generate() method.

Entry point Returns Available in
Canonical::generate() string PHP, Blade
canonical() string or the generator PHP, Blade
<x-canonical> <link> tag Blade
@canonical <link> tag Blade

Fkrzski\LaravelCanonical\Facades\Canonical.

Canonical::generate(?string $path = null): string
throws CanonicalConfigurationException

Builds the canonical URL for $path, joined onto the configured canonical domain. With no argument it canonicalises the current request URI, query string included.

use Fkrzski\LaravelCanonical\Facades\Canonical;
Canonical::generate(); // current request
Canonical::generate('/products/item'); // https://example.com/products/item
Canonical::generate('/'); // https://example.com
canonical(?string $path = null): CanonicalUrlGeneratorInterface|string
throws CanonicalConfigurationException

Called with a path it returns the URL string; called without arguments it returns the generator for fluent use.

$url = canonical('/products/item'); // "https://example.com/products/item"
$generator = canonical(); // fluent generator
$url = canonical()->generate('/products/item');

Fkrzski\LaravelCanonical\View\Components\Canonical, registered as x-canonical.

throws CanonicalConfigurationException

Renders a complete, HTML-escaped <link rel="canonical" href="…" /> tag.

Attribute Type Default Description
path ?string null Path to canonicalise. null, '', and '/' fall back to the bare domain.
<x-canonical />
<x-canonical path="/products/item" />
<x-canonical :path="$post->canonical_path" />
@canonical(?string $expression = null)
throws CanonicalConfigurationException

Echoes the same tag as the component, escaping the URL with e(). The optional expression is a path string or variable.

@canonical
@canonical('/products/item')
@canonical($canonicalPath)

Fkrzski\LaravelCanonical\Exceptions\CanonicalConfigurationException extends Exception. Thrown when the generator is first resolved from the container — in practice the first time you ask for a URL, including a bare canonical(). Raised when the domain is:

Condition Message
Unset or empty Canonical domain is not set in config.
Not a valid URL Invalid URL format: '…'. Expected a valid URL.
Scheme other than http/https Invalid URL scheme for '…'. Only 'http' and 'https' schemes are allowed.
Missing host Missing host in URL: '…'.

See domain validation for the accepted forms.

The deferred provider binds these contracts as singletons:

Contract Bound to
CanonicalUrlGeneratorInterface CanonicalUrlGenerator
CanonicalConfigInterface CanonicalConfig
CanonicalUrlBuilderInterface CanonicalUrlBuilder
BaseUrlValidatorInterface BaseUrlValidator

Rebind any of them to swap in your own implementation:

app/Providers/AppServiceProvider.php
$this->app->singleton(CanonicalUrlBuilderInterface::class, MyUrlBuilder::class);

Or resolve the generator directly instead of going through the facade:

$url = app(CanonicalUrlGeneratorInterface::class)->generate('/products/item');