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 |
Canonical facade
Section titled “Canonical facade”Fkrzski\LaravelCanonical\Facades\Canonical.
Canonical::generate(?string $path = null): stringBuilds 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 requestCanonical::generate('/products/item'); // https://example.com/products/itemCanonical::generate('/'); // https://example.comcanonical() helper
Section titled “canonical() helper”canonical(?string $path = null): CanonicalUrlGeneratorInterface|stringCalled 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');<x-canonical> component
Section titled “<x-canonical> component”Fkrzski\LaravelCanonical\View\Components\Canonical, registered as x-canonical.
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 directive
Section titled “@canonical directive”@canonical(?string $expression = null)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)CanonicalConfigurationException
Section titled “CanonicalConfigurationException”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.
Container bindings
Section titled “Container bindings”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:
$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');