Skip to main content

Introduction

Laravel provides several helpers to assist you in generating URLs for your application. These helpers are primarily useful when building links in your templates and API responses, or when generating redirect responses to another part of your application.

Basic usage

Generating URLs

You can use the url helper to generate arbitrary URLs. The generated URL will automatically use the scheme (HTTP or HTTPS) and host of the current request the application is handling.
To generate a URL with query string parameters, use the query method.
If you provide query string parameters that already exist in the path, the existing values will be overridden.
You can also pass array values as query parameters. These values will be properly keyed and encoded in the generated URL.

Accessing the current URL

If no path is provided to the url helper, an Illuminate\Routing\UrlGenerator instance is returned, allowing you to access information about the current URL.
Each of these methods may also be accessed via the URL facade.

Accessing the previous URL

Sometimes you want to know the URL of the previous page the user was visiting. You can use the previous or previousPath method on the url helper.
You can also retrieve the previous URL via the session.
You can also retrieve the route name of the previously visited URL via the session.

URLs for named routes

The route helper may be used to generate URLs to named routes. Named routes allow you to generate URLs without being coupled to the actual URL defined on the route. Therefore, if the route’s URL changes, no changes need to be made to your route function calls.
Generate a URL to this route as follows.
Routes with multiple parameters work as expected.
Any additional array elements that do not correspond to the route’s defined parameters will be added to the URL’s query string.

Eloquent models

You will often be generating URLs using the route key of an Eloquent model (usually its primary key). For this reason, you may pass Eloquent models as parameter values. The route helper automatically extracts the model’s route key.

Signed URLs

Laravel allows you to easily create “signed” URLs to named routes. These URLs have a “signature” hash appended to the query string, which allows Laravel to verify that the URL has not been modified since it was created. Signed URLs are especially useful for routes that are publicly accessible but need protection against URL manipulation. For example, you might use signed URLs to implement a public “unsubscribe” link that is sent to your customers via email. To create a signed URL to a named route, use the signedRoute method of the URL facade.
You can exclude the domain from the signed URL hash by providing the absolute argument to the signedRoute method.
If you want to generate a temporary signed route URL that expires after a specified amount of time, use the temporarySignedRoute method. When Laravel validates a temporary signed route URL, it ensures that the expiration timestamp encoded in the signed URL has not elapsed.

Signed URL validation flow

Validating signed route requests

To verify that an incoming request has a valid signature, call the hasValidSignature method on the Illuminate\Http\Request instance.
If you want to ignore certain query parameters during validation, use hasValidSignatureWhileIgnoring.
Instead of using the incoming request instance, you can assign the signed (Illuminate\Routing\Middleware\ValidateSignature) middleware to your route. If the incoming request does not have a valid signature, the middleware will automatically return a 403 HTTP response.
If your signed URLs do not include the domain, provide the relative argument to the middleware.

Responding to invalid signed routes

When someone visits an expired signed URL, they receive a generic error page for the 403 HTTP status code. You can customize this by defining a custom “render” closure for the InvalidSignatureException exception in your application’s bootstrap/app.php file.

URLs for controller actions

The action function generates a URL for the given controller action.
If the controller method accepts route parameters, pass an associative array of parameters as the second argument to the function.

Fluent URI objects

Laravel’s URI class provides a convenient, fluent interface for creating and manipulating URIs via objects.
Once you have a URI instance, you can modify it fluently.

Default URL parameters

Sometimes you may wish to specify request-wide default values for certain URL parameters. For example, imagine many of your routes have a {locale} parameter.
It’s cumbersome to pass the locale every time you call the route helper. So you can use the URL::defaults method to define a default value for this parameter that will always be applied during the current request. You may want to call this method from a route middleware so you have access to the current request.
Once the default value for the locale parameter has been set, you no longer need to pass its value when generating URLs via the route helper.
Setting URL defaults may interfere with Laravel’s handling of implicit model bindings. Therefore, you should prioritize your middleware so that middleware that set URL defaults are executed before Laravel’s own SubstituteBindings middleware. You can accomplish this using the priority middleware method in your application’s bootstrap/app.php file.
Last modified on July 20, 2026