Everything routes through one generator: the facade, the helper, the Blade component, and the directive all build the same string the same way.
Generating a URL
Section titled “Generating a URL”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/postCanonical::generate('/products/item'); // https://example.com/products/itemcanonical()->generate(); // request /blog/post → https://example.com/blog/postcanonical('/products/item'); // https://example.com/products/itemThe 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.
How a path is normalised
Section titled “How a path is normalised”- A leading slash is optional —
generate('blog')andgenerate('/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.comCanonical::generate('/blog/'); // https://example.com/blogCanonical::generate('blog'); // https://example.com/blogCanonical::generate('/'); // https://example.comCanonical::generate('/blog/post/123'); // https://example.com/blog/post/123Query parameters
Section titled “Query parameters”generate() reads the request URI, not just the path, so query strings on the
current request are carried through:
// Request: /search?q=laravel&page=2Canonical::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=laravelThe configuration exception
Section titled “The configuration exception”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.