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 theurl 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.
query method.
Accessing the current URL
If no path is provided to theurl helper, an Illuminate\Routing\UrlGenerator instance is returned, allowing you to access information about the current URL.
URL facade.
Accessing the previous URL
Sometimes you want to know the URL of the previous page the user was visiting. You can use theprevious or previousPath method on the url helper.
URLs for named routes
Theroute 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.
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. Theroute 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 thesignedRoute method of the URL facade.
absolute argument to the signedRoute method.
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 thehasValidSignature method on the Illuminate\Http\Request instance.
hasValidSignatureWhileIgnoring.
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.
relative argument to the middleware.
Responding to invalid signed routes
When someone visits an expired signed URL, they receive a generic error page for the403 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
Theaction function generates a URL for the given controller action.
Fluent URI objects
Laravel’s URI class provides a convenient, fluent interface for creating and manipulating URIs via objects.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.
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.
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.