Backend Development 5 min read

Generating URLs in Laravel with Helper Functions

This article explains Laravel's URL helper functions, showing how to generate basic URLs, retrieve the current URL, create URLs for named routes—including routes with parameters and Eloquent models—and produce signed and temporary signed URLs for secure link handling.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Generating URLs in Laravel with Helper Functions

Laravel provides several helper functions for generating URLs within an application.

Generating Basic URLs

The url helper can be used for any URL. It automatically uses the current request scheme (HTTP or HTTPS) and host.

$post = App\Models\Post::find(1);

echo url("/posts/{$post->id}");
// http://example.com/posts/1

Accessing the Current URL

If the url helper is called without a path, it returns an Illuminate\Routing\UrlGenerator instance, allowing access to information about the current URL.

// Get current URL without query string
echo url()->current();

// Get current URL with query string
echo url()->full();

// Get previous request URL
echo url()->previous();
use Illuminate\Support\Facades\URL;

echo URL::current();

Named Route URLs

The route helper generates URLs for named routes, decoupling the generated URL from the route definition.

Route::get('/post/{post}', function (Post $post) {
    // ...
})->name('post.show');
echo route('post.show', ['post' => 1]);
// http://example.com/post/1

The route helper also works with routes that have multiple parameters.

Route::get('/post/{post}/comment/{comment}', function (Post $post, Comment $comment) {
    // ...
})->name('comment.show');

echo route('comment.show', ['post' => 1, 'comment' => 3]);
// http://example.com/post/1/comment/3

Eloquent Models

You can pass an Eloquent model to the route helper; it will automatically extract the model's primary key.

echo route('post.show', ['post' => $post]);

Signed URLs

Laravel can create "signed" URLs that include a hash in the query string, allowing verification that the URL has not been altered. Signed URLs are useful for publicly accessible routes that need protection.

use Illuminate\Support\Facades\URL;

return URL::signedRoute('unsubscribe', ['user' => 1]);

For temporary signed URLs that expire, use temporarySignedRoute :

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 hasValidSignature on the Illuminate\Http\Request instance.

use Illuminate\Http\Request;

Route::get('/unsubscribe/{user}', function (Request $request) {
    if (! $request->hasValidSignature()) {
        abort(401);
    }
    // ...
})->name('unsubscribe');
BackendRoutingPHPURLLaravelSigned URLs
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.