Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Generating URLs in Laravel with Helper Functions

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 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');
About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
routingPHPURLLaravelSigned URLs

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion