#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Route Parameters and Named Routes

Real applications rarely have only static URLs — a blog needs a route per post, a shop needs a route per product. Laravel handles this through route parameters, and it lets developers reference routes by name instead of hardcoding URLs everywhere.

Route Parameters

A route parameter is a placeholder in the URL pattern that captures part of the request and passes it into the controller or closure. Parameters can be required or optional, and Laravel supports constraining them with regular expressions when needed.

Named Routes

Rather than typing out full URLs throughout an application, Laravel lets you assign a name to a route and generate links or redirects using that name instead. If the underlying URL ever changes, every reference automatically updates, since nothing depends on the literal path.

SyntaxExample
Required parameterRoute::get('/post/{id}', ...)
Optional parameterRoute::get('/post/{id?}', ...)
Naming a routeRoute::get('/post/{id}', ...)->name('post.show')
Generating a URL from a nameroute('post.show', ['id' => 1])

Why This Matters

Routes work well for small amounts of logic, but as an application grows, that logic needs a proper home — which is exactly the role controllers play.

Constraints, Defaults and Route Order

Parameters are passed to your closure or controller in the order they appear in the URL. Constraint helpers make sure the value has the shape you expect before your code even runs.

Route::get('/post/{id}', [PostController::class, 'show'])
    ->whereNumber('id')
    ->name('post.show');

Route::get('/user/{name?}', function (?string $name = 'guest') {
    return "Hello, {$name}";
});

Route::get('/archive/{year}/{slug}', [ArchiveController::class, 'show'])
    ->where(['year' => '[0-9]{4}', 'slug' => '[a-z0-9-]+']);

An optional parameter must always have a default value in the function signature, otherwise Laravel cannot call the closure when the segment is missing.

Order also matters. If /post/{id} is registered before /post/create and has no constraint, the word "create" will be captured as an ID. Either register fixed routes first or add a constraint such as whereNumber().

Letting Laravel Fetch the Model for You

Route model binding removes the usual "find the record or return 404" boilerplate. When the parameter name matches a type-hinted model variable, Laravel loads the record automatically and returns a 404 page if it does not exist.

Route::get('/posts/{post}', function (Post $post) {
    return view('posts.show', ['post' => $post]);
})->name('posts.show');

// Look up by a different column, such as a slug
Route::get('/blog/{post:slug}', [PostController::class, 'show'])->name('blog.show');

Binding by slug produces friendlier URLs, but the column should be unique and indexed so lookups stay fast.

Binding also works inside controllers. A method such as public function show(Post $post) receives the loaded model directly, so the method body can focus on what to display instead of how to find the record. For nested resources, scoped binding can check that a child belongs to its parent, for example that a comment really belongs to the post named in the same URL.

Working With Named Routes Day to Day

// Redirect after saving
return redirect()->route('posts.show', $post);

// Blade link
<a href="{{ route('posts.show', $post) }}">Read more</a>

// Group names with a prefix
Route::prefix('admin')->name('admin.')->group(function () {
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    // full name: admin.users.index
});

// Highlight the active menu item
@if (request()->routeIs('admin.users.*')) ... @endif

You can pass a model straight to route(); Laravel uses its route key, which is the ID unless you change it. Extra array items that are not route parameters become query string values, so route('posts.index', ['page' => 2]) produces ?page=2.

Group prefixes keep names consistent. A common convention follows the resource pattern: posts.index, posts.create, posts.store, posts.show, posts.edit, posts.update and posts.destroy. Using Route::resource() creates all of these names for you.

Common Errors and a Quick Checklist

  • "Missing required parameter" error: you called route() without a value the URL needs.
  • "Route not defined" error: a typo in the name, or the route cache is stale; run php artisan route:clear.
  • Duplicate names: the later route silently wins, so links point somewhere unexpected.
  • Hardcoded URLs: any href="/post/5" left in views will break when paths change.

Before moving on, run php artisan route:list --name=post to confirm each route has the URL, name and middleware you intended.

Ready to master Laravel Training Course?

Join Uncodemy's hands-on Laravel program and build real, production-ready applications.

Explore Course