Originally published on laravel.learnio.dev — part of the Laravel best practices course. View this lesson on the original site.
Keep URL changes from breaking your app
Named routes let you generate URLs by route name, not by manually typed strings. This keeps links stable when URIs change.
Define route names consistently
use App\Http\Controllers\ProductController;
use Illuminate\Support\Facades\Route;
Route::get('/products', [ProductController::class, 'index'])
->name('products.index');
Route::get('/products/{product}', [ProductController::class, 'show'])
->name('products.show');
A common pattern is resource.action such as products.index or orders.show.
Generate URLs and redirects safely
Generate a URL:
$url = route('products.show', ['product' => 42]);
Redirect by name:
return redirect()->route('products.show', ['product' => $product->id]);
Or use the shortcut:
return to_route('products.show', ['product' => $product->id]);
Route parameters and query strings
Named route parameters map by key:
route('products.show', ['product' => 42]);
Extra keys become query string values:
route('products.show', ['product' => 42, 'tab' => 'pricing']);
// /products/42?tab=pricing
You can also pass models directly, Laravel uses the model route key:
route('products.show', ['product' => $product]);
Bad way vs better way
Bad way (hard-coded URI strings across views/controllers):
return redirect('/products/' . $product->id);
Better way (named route contract):
return to_route('products.show', ['product' => $product->id]);
Why better:
- path changes in one place only,
- less fragile string concatenation,
- easier refactors and safer tests.
Check names during development
Use:
php artisan route:list --name=products
This quickly confirms route names, URIs, methods, and actions are aligned.
URL generation flow
flowchart LR
A[Route name and params] --> B[route helper resolves route]
B --> C[Concrete URL]
C --> D[Link or redirect response]
Next, you will group related routes with shared prefixes and middleware boundaries.
From Laravel best practices — Routing, Controllers & Request Flow