Everything routes through one generator: the facade, the helper, the Blade component, and the directive all build the same string the same way.

Call generate() with no argument to canonicalise the current request, or pass a path to override it. The helper is a shortcut that resolves the same generator out of the container:

use Fkrzski\LaravelCanonical\Facades\Canonical;
Canonical::generate(); // request /blog/post → https://example.com/blog/post
Canonical::generate('/products/item'); // https://example.com/products/item

The path is joined onto the configured canonical domain, never the host the request arrived on — so a request to a staging or www host still emits the canonical apex URL.

  • A leading slash is optional — generate('blog') and generate('/blog') are equivalent.
  • A trailing slash is trimmed by default — see trailing-slash handling.
  • The root path collapses to the bare domain, with no trailing slash.
  • Query parameters are preserved — they are part of the path and pass through untouched.
// Domain: https://example.com
Canonical::generate('/blog/'); // https://example.com/blog
Canonical::generate('blog'); // https://example.com/blog
Canonical::generate('/'); // https://example.com
Canonical::generate('/blog/post/123'); // https://example.com/blog/post/123

generate() reads the request URI, not just the path, so query strings on the current request are carried through:

// Request: /search?q=laravel&page=2
Canonical::generate(); // https://example.com/search?q=laravel&page=2
// An explicit path overrides the request entirely.
Canonical::generate('/search?q=laravel'); // https://example.com/search?q=laravel

The domain is validated on first use, not at boot. An unset or malformed domain throws CanonicalConfigurationException rather than emitting a broken tag:

use Fkrzski\LaravelCanonical\Exceptions\CanonicalConfigurationException;
try {
Canonical::generate('/page');
} catch (CanonicalConfigurationException $e) {
// Domain missing, or not a valid http/https URL.
}

Which values are accepted is covered in domain validation.