11 Laravel 11 Basics: URL Generation

Laravel provides several helpers to assist you in generating URLs for your application. These helpers are helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.
Generating URLs
The url 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:
$post = App\Models\Post::find(1);
echo url("/posts/{$post->id}");
// http://example.com/posts/1To generate a URL with query string parameters, you may use the query method:
echo url()->query('/posts', ['search' => 'Laravel']);
// https://example.com/posts?search=LaravelProviding query string parameters that already exist in the path will overwrite their existing value:
echo url()->query('/posts?sort=latest', ['sort' => 'oldest']);
// http://example.com/posts?sort=oldestAccessing 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:
echo url()->current(); // Get the current URL without the query string
echo url()->full(); // Get the current URL including the query string
echo url()->previous(); // Get the full URL for the previous requestURLs 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. For example, imagine your application contains a route defined like the following:
Route::get('/post/{post}', function (Post $post) {
// ...
})->name('post.show');To generate a URL to this route, you may use the route helper:
echo route('post.show', ['post' => 1]);
// http://example.com/post/1Any additional array elements that do not correspond to the route's definition parameters will be added to the URL's query string:
echo route('post.show', ['post' => 1, 'search' => 'rocket']);
// http://example.com/post/1?search=rocketSigned 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. To create a signed URL to a named route, use the signedRoute method of the URL facade:
use Illuminate\Support\Facades\URL;
return URL::signedRoute('unsubscribe', ['user' => 1]);To generate a temporary signed route URL that expires after a specified amount of time, use the temporarySignedRoute method:
use Illuminate\Support\Facades\URL;
return URL::temporarySignedRoute('unsubscribe', now()->addMinutes(30), ['user' => 1]);Validating Signed Route Requests
To verify that an incoming request has a valid signature, call the hasValidSignature method on the incoming Illuminate\Http\Request instance:
use Illuminate\Http\Request;
Route::get('/unsubscribe/{user}', function (Request $request) {
if (! $request->hasValidSignature()) {
abort(401);
}
// ...
})->name('unsubscribe');URLs for Controller Actions
The action function generates a URL for the given controller action:
use App\Http\Controllers\HomeController;
$url = action([HomeController::class, 'index']);If the controller method accepts route parameters, you may pass an associative array of route parameters as the second argument to the function:
$url = action([UserController::class, 'profile'], ['id' => 1]);Default Values
You may specify request-wide default values for certain URL parameters using the URL::defaults method. For example, you may wish to set a default value for the {locale} parameter in your routes:
$request->user()->locale
]);
return $next($request);
}
}Conclusion
Using Laravel's URL generation helpers can simplify your application's routing and navigation. Whether you're generating links in templates or creating secure, signed URLs, these tools help you build a robust and maintainable application. For more detailed information, refer to the official Laravel URL Generation documentation.
Learn more about URL generation in Laravel 11 and enhance your application's routing capabilities. Check out the official documentation for more examples and advanced usage.
Discuss Your Project with Us
We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.
Let's find the best solutions for your needs.