Laravel Routing Basics: Directing Traffic in Your App

January 7, 2024 · 5 min read

2026 UPDATE - LARAVEL 13
We’re excited to announce that we have updated all of our blog post examples to reflect the new Laravel 13 version! Our previous examples were based on Laravel 10, but with the release of Laravel 13, we wanted to ensure that our readers have access to the most up-to-date information and examples.
What is Routing?
Routing in Laravel is the process of mapping HTTP requests to application logic that handles those requests. When a user clicks a link or types a URL into their browser, Laravel’s routing system determines which controller and method should respond. It’s like a GPS for your web application, directing traffic to the right destination.
If you’re new to this framework, understanding how Laravel handles routing is crucial for creating a seamless and intuitive user experience. In this article, we’ll explore the essence of Laravel routing using a blog application as our example.
Defining Routes
Routes in Laravel are defined in the routes/web.php file. This file is where you tell Laravel what to do when it receives a specific type of request (GET, POST, PUT, DELETE, etc.) to a specific URL.
Let’s look at some basic route definitions for our blog application:
Displaying All Blog Posts
To show all blog posts, we need to add the following route:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);Here, Route::get tells Laravel to respond to a GET request. /posts is the URI the user visits, and [PostController::class, 'index'] points to the index method in the PostController which will handle the request. Make sure you also import the PostController with use App\Http\Controllers\PostController;.
Showing a Single Blog Post
To display a single blog post, we would use a route with a parameter:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);The {post} is a route parameter that Laravel will automatically pass to the show method of the PostController, so it knows which post to display.
Creating a New Blog Post
For creating a new post, we would have two routes: one for showing the form and another for handling the form submission.
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);The route /posts/create responds to a GET request and shows the user a form. The second route responds to a POST request when the user submits the form, calling the store method to save the new post.
We reordered the routes to ensure they function correctly. It’s crucial to maintain this order; otherwise, it may result in failure. For instance, the last route, which depends on the {post} parameter (which can take any value), should be placed below other routes like /posts/create.
Editing an Existing Blog Post
Editing a post also requires two routes:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::get('/posts/{post}/edit', [PostController::class, 'edit']);
Route::put('/posts/{post}', [PostController::class, 'update']);The edit route displays the edit form, and the update route processes the form submission to update the post.
Deleting a Blog Post
To delete a post, you would use a DELETE request:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/create', [PostController::class, 'create']);
Route::post('/posts', [PostController::class, 'store']);
Route::get('/posts/{post}', [PostController::class, 'show']);
Route::get('/posts/{post}/edit', [PostController::class, 'edit']);
Route::put('/posts/{post}', [PostController::class, 'update']);
Route::delete('/posts/{post}', [PostController::class, 'destroy']);This route calls the destroy method in the PostController to delete the specified post.
Resourceful Routing
Laravel provides a shortcut to create all the routes for these standard actions with a single line of code using a resource route:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
// with the resource controller we have everything in one line
Route::resource('posts', PostController::class);This single line will generate all the necessary CRUD routes for the PostController. This is the command that creates a resource controller with all the methods:
php artisan make:controller PostController --resource

API Routes in Laravel 13
In Laravel 11 and later, routes/api.php is not created by default. If your application needs API routes, run the following command to scaffold the file:
php artisan install:api
This creates routes/api.php and registers it automatically. All routes defined there are prefixed with /api and have the api middleware group applied.
Applying Middleware on Controllers in Laravel 13
In Laravel 11 and later, you can apply middleware directly on a controller class or individual methods using the #[Middleware] PHP attribute, as an alternative to chaining ->middleware() on route definitions:
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Routing\Controllers\Middleware;
class PostController extends Controller implements HasMiddleware
{
public static function middleware(): array
{
return [
new Middleware('auth', only: ['create', 'store', 'edit', 'update', 'destroy']),
];
}
// controller methods...
}This keeps route protection co-located with the controller. Your route definitions in routes/web.php remain clean and unchanged.
Route Parameters
Laravel routes can have parameters, as we saw with the {post} parameter in some of the examples above. These parameters are passed to the controller methods, allowing you to handle dynamic data, such as identifying which blog post to show or edit.
Route::get('/posts/{post}', [PostController::class, 'show']);
Named Routes
Laravel routes can also be named, which makes it easier to generate URLs or redirects within the application. Here’s how you can name a route:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/posts', [PostController::class, 'index'])
->name('posts.index');
Route::get('/posts/create', [PostController::class, 'create'])
->name('posts.create');
Route::post('/posts', [PostController::class, 'store'])
->name('posts.store');
Route::get('/posts/{post}', [PostController::class, 'show'])
->name('posts.show');
Route::get('/posts/{post}/edit', [PostController::class, 'edit'])
->name('posts.edit');
Route::put('/posts/{post}', [PostController::class, 'update'])
->name('posts.update');
Route::delete('/posts/{post}', [PostController::class, 'destroy'])
->name('posts.destroy');You can refer to these routes using their name. We already mentioned this when we discussed the Processing Form Input section while learning about the MVC pattern. For example, if we want to redirect the user to the index page:
return redirect()->route('posts.index');
But, if you use a resource controller then you don’t need to manually add all the route names because they’re automatically created. That’s why we strongly suggest that you use resource controllers as much as possible.

Conclusion
Laravel’s routing system is powerful and flexible, allowing you to define clear paths for every action in your application. By understanding routes, you can control how your application responds to user requests and guide users through the features and pages of your blog.
Upcoming Articles in the Series
This article is part of our series Laravel for Beginners: A Step-by-Step Guide to Learn the Concepts.
Bring Your Ideas to Life 🚀
If you need help with a Laravel project let’s get in touch.
Lucky Media is proud to be recognized as a leading Laravel Development Agency
Technologies

Stay up-to-date
Be updated with all news, products and tips we share!
On this page
- 2026 UPDATE - LARAVEL 13
- What is Routing?
- Defining Routes
- Displaying All Blog Posts
- Showing a Single Blog Post
- Creating a New Blog Post
- Editing an Existing Blog Post
- Deleting a Blog Post
- Resourceful Routing
- API Routes in Laravel 13
- Route Parameters
- Named Routes
- Conclusion
- Upcoming Articles in the Series
- Bring Your Ideas to Life 🚀

