While building Laravel applications, you will encounter a task in which you'll need to generate URLs. Laravel has some very useful helpers that can assist you to generate URLs. These helpers can help you build links for the templates and API responses, or to generate a redirect response to another part of the application. Before we take a look at the different types of Laravel URLs, we will start with the basics.
Table of Contents
Basics
The basic way to generate basic URLs, is to use the Laravel helper function url()
.
$book = App\Book::get('id', '=', 3)->first();
echo url("/books/{$book->id}"); // http://localhost:8000/books/3
There is also the possibility to access the current URL with or without the query string, or to get the full URL of the previous request.
// current URL without the query string
echo url()->current();
// current URL with the query string
echo url()->full();
// full URL of the previous request
echo url()->previous();
Laravel also has a Facade URL, that can also provide functionality the same functionality as the helper function url()
to get the current URL.
use Illuminate\Support\Facades\URL;
echo URL::current();
URL for Named Routes
Named routes allows to chain a name onto the route definition. The name route provide helpful generation of URLs or they can be used for redirects for specific routes. So, if the route's URL changes, there is no need to change anything, because we are using the named routes.
For example, let's say that we have defined a route to show details of a book, and we have named that route book.show
Route::get('/books/{book}', [BookController::class, 'show'])->name('book.show');
To generate a URL for this route, we can use the route helper.
echo route('book.show', ['book' => 3]);
// http://example.com/book/3
The route function accept parameters, that is placed in the correct positions. If there are passed additional parameters, they will be automatically added to the URL's query string.
echo route('book.show', ['book' => 3, 'type' => 'romance']);
// http://example.com/book/3?type=romance
Signed URLs
Laravel provides functionality to create signed URL's from named routes. Signed URL is used for a publicly accessible URL that needs to be protected from manipulation. Laravel verifies the hash that is appended in the query string to make sure that is not modified.
For example, let's say that we want to create a temporary signed URL that user can use to apply a promotion code. We will add a expiration timestamp that will be validated to make sure that the timestamp has not elapsed.
use Illuminate\Support\Facades\URL;
return URL::temporarySignedRoute(
'subscribe', now()->addMinutes(30), ['user' => 2, 'code' => 'DISCOUNT20']
);
To validate the signed URL, Laravel provide hasValidSignature method. You can read more on the official documentation.
URL for Controller Actions
To generate a URL for a specific Controller action, Laravel provides the action()
helper, that also accepts an associative array of parameters if the controller action accepts route parameters.
use App\Http\Controllers\BookController;
$indexUrl = action([BookController::class, 'index']);
$showUrl = action([BookController::class, 'show'], ['id' => 1]);
Conclusion
In this article, we took an overview of the different types of URL generation in Laravel 10. We gave examples of how to generate signed URLs and how to use generate ULR with named routes. To dive deeper in URL Generation in Laravel 10, check the official documentation.