Laravel Routing: All You Need to Know About GET, POST, and Resource Routes

Published on November 04, 2025
Laravel Routing PHP WebDevelopment RESTful InterviewPrep

What is Routing in Laravel?

Routing is the mechanism that maps URLs to your application's logic. Think of routes as "directions" that tell Laravel what to do when someone visits a specific URL in your application.

In simple terms: Routes define the endpoints of your application and what happens when users visit them.

Basic Routing Setup

All web routes are defined in routes/web.php file. Let's start with the most basic route.

The Default Route

// routes/web.php
Route::get('/', function () {
    return 'Welcome to TeachyLeaf Blog!';
});

When you visit http://your-app.com/, you'll see "Welcome to TeachyLeaf Blog!"

HTTP Methods in Laravel Routing

Laravel supports all HTTP methods. Here are the most common ones:

1. GET Routes - For Reading Data

Route::get('/about', function () {
    return 'About Us Page';
});

Route::get('/contact', function () {
    return view('contact');
});

2. POST Routes - For Creating Data

Route::post('/contact', function () {
    // Process form submission
    return 'Form submitted successfully!';
});

3. PUT/PATCH Routes - For Updating Data

Route::put('/users/{id}', function ($id) {
    // Update user
    return "User {$id} updated";
});

Route::patch('/profile/{id}', function ($id) {
    // Partial update
    return "Profile {$id} updated";
});

4. DELETE Routes - For Removing Data

Route::delete('/users/{id}', function ($id) {
    // Delete user
    return "User {$id} deleted";
});

Route Parameters: Dynamic URLs

Basic Route Parameters

// Single parameter
Route::get('/users/{id}', function ($id) {
    return "User ID: {$id}";
});

// Multiple parameters
Route::get('/posts/{category}/{slug}', function ($category, $slug) {
    return "Category: {$category}, Slug: {$slug}";
});

Optional Parameters

Route::get('/users/{id?}', function ($id = null) {
    return $id ? "User ID: {$id}" : 'All Users';
});

Parameter Constraints with Regular Expressions

Route::get('/users/{id}', function ($id) {
    return "User ID: {$id}";
})->where('id', '[0-9]+'); // Only numeric IDs

Route::get('/posts/{slug}', function ($slug) {
    return "Post: {$slug}";
})->where('slug', '[A-Za-z-]+'); // Only letters and hyphens

Named Routes: Give Your Routes a Name

Named routes make it easy to generate URLs throughout your application.

Defining Named Routes

Route::get('/home', function () {
    return view('home');
})->name('homepage');

Route::get('/users/profile', function () {
    return view('profile');
})->name('profile');

Generating URLs for Named Routes

// In controllers
return redirect()->route('homepage');

// In Blade templates
<a href="{{ route('homepage') }}">Home</a>
<a href="{{ route('profile') }}">My Profile</a>

// With parameters
Route::get('/users/{id}', function ($id) {
    // ...
})->name('users.show');

// Generate URL with parameter
route('users.show', ['id' => 1]); // Returns /users/1

Route Groups: Organizing Your Routes

Route groups allow you to apply attributes to multiple routes.

Prefix Groups

// Admin routes with /admin prefix
Route::prefix('admin')->group(function () {
    Route::get('/users', function () {
        return 'Admin Users Page';
    });
    
    Route::get('/settings', function () {
        return 'Admin Settings';
    });
});
// URLs: /admin/users, /admin/settings

Middleware Groups

// Apply auth middleware to multiple routes
Route::middleware(['auth'])->group(function () {
    Route::get('/dashboard', function () {
        return 'User Dashboard';
    });
    
    Route::get('/profile', function () {
        return 'User Profile';
    });
});

Name Prefix Groups

Route::name('admin.')->group(function () {
    Route::get('/users', function () {
        // ...
    })->name('users'); // Named route: admin.users
    
    Route::get('/settings', function () {
        // ...
    })->name('settings'); // Named route: admin.settings
});

Controller Routes: Connecting to Controllers

Instead of putting logic in route files, connect routes to controller methods.

Basic Controller Routes

// Single method
Route::get('/users', [UserController::class, 'index']);

// Multiple methods
Route::get('/users/{id}', [UserController::class, 'show']);
Route::post('/users', [UserController::class, 'store']);
Route::put('/users/{id}', [UserController::class, 'update']);
Route::delete('/users/{id}', [UserController::class, 'destroy']);

Resource Routes: The Power of RESTful Routing

Resource routes create multiple routes for a controller with a single line.

Basic Resource Route

Route::resource('posts', PostController::class);

This single line creates 7 routes automatically:

HTTP Method URL Controller Method Route Name
GET /posts index posts.index
GET /posts/create create posts.create
POST /posts store posts.store
GET /posts/{id} show posts.show
GET /posts/{id}/edit edit posts.edit
PUT/PATCH /posts/{id} update posts.update
DELETE /posts/{id} destroy posts.destroy

API Resource Routes (Without create/edit)

Route::apiResource('posts', PostController::class);

Creates only: index, store, show, update, destroy (perfect for APIs).

Multiple Resource Controllers

Route::resources([
    'posts' => PostController::class,
    'users' => UserController::class,
    'comments' => CommentController::class
]);

Partial Resource Routes

// Only these methods
Route::resource('posts', PostController::class)->only([
    'index', 'show', 'store'
]);

// Exclude these methods
Route::resource('posts', PostController::class)->except([
    'create', 'edit', 'update', 'destroy'
]);

Practical Example: Blog Application Routes

Here's a complete example for a blog:

// routes/web.php

// Public routes
Route::get('/', [PostController::class, 'index'])->name('home');
Route::get('/about', function () {
    return view('about');
})->name('about');

// Post routes
Route::get('/posts/{slug}', [PostController::class, 'show'])->name('posts.show');
Route::get('/categories/{category}', [PostController::class, 'byCategory'])->name('posts.category');

// Authentication routes (provided by Laravel)
Auth::routes();

// Protected admin routes
Route::middleware(['auth'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminController::class, 'dashboard'])->name('dashboard');
    
    // Resource routes for CRUD operations
    Route::resource('posts', AdminPostController::class);
    Route::resource('categories', CategoryController::class);
});

// API routes (in routes/api.php)
Route::prefix('api')->group(function () {
    Route::apiResource('posts', ApiPostController::class);
    Route::get('search', [ApiPostController::class, 'search']);
});

Route Model Binding: Elegant Parameter Handling

Laravel can automatically inject model instances based on route parameters.

Implicit Route Model Binding

// Laravel automatically finds the Post model
Route::get('/posts/{post}', function (Post $post) {
    return view('posts.show', compact('post'));
});

// With custom column
Route::get('/posts/{post:slug}', function (Post $post) {
    return view('posts.show', compact('post'));
});

Customizing Route Model Binding

// In RouteServiceProvider.php
public function boot()
{
    Route::model('user', User::class);
    
    // Or with custom logic
    Route::bind('post', function ($value) {
        return Post::where('slug', $value)->firstOrFail();
    });
}

Common Interview Questions & Answers

1. What is Laravel routing?
Laravel routing maps URLs to controller actions or closures, defining how your application responds to HTTP requests.

2. What's the difference between GET and POST routes?
GET requests are for retrieving data and should be idempotent. POST requests are for creating or modifying data and can have side effects.

3. What are resource controllers?
Resource controllers provide built-in methods for CRUD operations (index, create, store, show, edit, update, destroy) that can be mapped with a single route declaration.

4. How do you create a named route?
Use the name() method: Route::get('/about', fn() => 'About')->name('about');

5. What is route model binding?
Route model binding automatically injects model instances into your routes based on the route parameters, eliminating the need to manually fetch models.

6. How do you group routes?
Use Route::group() with attributes like prefix, middleware, or name prefix to apply common settings to multiple routes.

Best Practices

  • Use resource routes for CRUD operations
  • Name your routes for easy URL generation
  • Keep route files organized with groups
  • Use middleware for authentication and authorization
  • Validate route parameters with constraints
  • Use route caching in production:
    php artisan route:cache

Quick Reference Cheat Sheet

// Basic routes
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::any($uri, $callback); // Any method

// Resource routes
Route::resource($uri, $controller);
Route::apiResource($uri, $controller);

// Route parameters
Route::get('user/{id}', fn($id) => $id);
Route::get('user/{id?}', fn($id = 1) => $id);

// Named routes
Route::get('about', fn() => 'About')->name('about');

// Groups
Route::prefix('admin')->group(fn() => {});
Route::middleware('auth')->group(fn() => {});

Now you're ready to handle any routing scenario in Laravel! In our next post, we'll dive into Laravel Controllers: Creating and Using Them to Handle Logic where you'll learn to organize your application logic properly.