Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Links

Which approach should I prefer when dealing with links in both the production and the development environments?

url('/something')

Or

route('route.name', [$var1, $var2])

I am leaning towards the named route approach, but not quite sure.

1 like
3 replies
Glukinho's avatar
Level 31

Use named routes, they are convenient and you can manipulate models, not IDs. If you have a route:

Route::put('/book/{book}', [ BookController::class, 'update' ])->name('book.update');

and a controller:

// BookController.php

public function update(Book $book)
{
    // ...
}

then this:

$book = Book::where('name', 'Harry Potter and whatever')->first();

route('book.update', ['book' => $book])

will generate this URL: https://yoursite.com/book/123 (assuming 123 is the id of the book "Harry Potter and whatever")

1 like
J8RO8N's avatar

I agree with @glukinho. If you use the named routes you are more flexible in the future.

Please or to participate in this conversation.