Introduction
Laravel provides several helpers to assist you in generating URLs for your application. These helpers are primarily helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.The Basics
Generating URLs
Theurl helper may be used to generate arbitrary URLs for your application. The generated URL will automatically use the scheme (HTTP or HTTPS) and host from the current request being handled by the application.
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
You can access the previous URL via theurl helper’s previous and previousPath methods:
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 calls to the route function.
route helper:
route helper may also be used to generate URLs for routes with multiple parameters:
Eloquent Models
You may pass Eloquent models as parameter values. Theroute helper will automatically extract 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 yet need a layer of protection against URL manipulation. For example, you might use signed URLs to implement a public “unsubscribe” link that is emailed to your customers. To create a signed URL to a named route, use thesignedRoute method of the URL facade:
absolute argument:
temporarySignedRoute method:
Signed URL Validation Flow
Validating Signed Route Requests
To verify that an incoming request has a valid signature, you should call thehasValidSignature method on the incoming Illuminate\Http\Request instance:
hasValidSignatureWhileIgnoring method:
signed (Illuminate\Routing\Middleware\ValidateSignature) middleware to the 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 a signed URL that has expired, they will receive a generic error page for the403 HTTP status code. You can customize this behavior 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’sUri class provides a convenient and fluent interface for creating and manipulating URIs via objects.
Default Values
For some applications, you may wish to specify request-wide default values for certain URL parameters. For example, imagine many of your routes define a{locale} parameter:
locale every time you call the route helper. So, you may use the URL::defaults method to define a default value for this parameter that will always be applied during the current request. You may wish to call this method from a route middleware so that you have access to the current request:
locale parameter has been set, you are no longer required to pass its value when generating URLs via the route helper.
Setting URL default values can interfere with Laravel’s handling of implicit model bindings. Therefore, you should prioritize your middleware that set URL defaults to be executed before Laravel’s own
SubstituteBindings middleware. You can accomplish this using the priority middleware method in your application’s bootstrap/app.php file: