Defining Basic Web Routes in Laravel: A Beginner's Guide
Master Laravel routing by learning how to map URLs to actions in web.php. This guide covers defining GET routes and returning responses for your app.
Previously in this course, we finished initializing the Task Manager project and verified that our local server was running. Now that we have a blank canvas, it's time to define how users interact with our application.
Routing is the entry point for every request hitting your Laravel application. Think of it as a traffic controller: when a user visits a specific URL, the router decides which piece of code should handle that request. If you've ever wrestled with complex systems like the WordPress Rewrite API, you'll find Laravel’s approach refreshing, clean, and explicit.
Understanding the web.php File
In Laravel, all web-based routes are defined within the routes/web.php file. When a request enters your application, Laravel consults this file to match the URL against a defined route.
If you open routes/web.php in your project, you'll see a default route already defined:
PHPuse Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); });
This is the most fundamental example of URL mapping. It tells Laravel: "When a user sends a GET request to the root path (/), execute this function and return the result."
The Anatomy of a Route
A basic route definition consists of three parts:
- The HTTP Verb:
Route::get,Route::post,Route::put, orRoute::delete. For simply viewing pages, we useGET. - The URI: The part of the URL after your domain (e.g.,
/tasksor/about). - The Action: Usually a closure (an anonymous function) or a controller method that tells the app what to do.
Defining Your First Custom Route
Let's add a route to our Task Manager to display a simple "Hello" message. Open routes/web.php and add this line below the existing route:
PHPRoute::get('/hello', function () { return 'Hello, Task Manager!'; });
Save the file and navigate to http://127.0.0.1:8000/hello in your browser. You should see the plain text "Hello, Task Manager!" rendered on the screen.
Why does this work?
When you hit that URL, the Laravel application lifecycle—which we discussed in the Laravel application lifecycle—triggers. The router inspects the request, finds a match for /hello in web.php, and executes the closure. Whatever you return from that closure is automatically converted into an HTTP response by Laravel.
Hands-on Exercise
To get comfortable with the syntax, let's expand our project's routing:
- Open
routes/web.php. - Define a new route for
/aboutthat returns the string "This is a simple Task Manager built with Laravel." - Define a new route for
/contactthat returns "Email us at support@taskmanager.test". - Visit these URLs in your browser to verify they work.
Common Pitfalls
Even experienced developers trip up on routing occasionally. Here are the most common issues:
- Route Order: Laravel matches routes from top to bottom. If you have a catch-all route at the top of your file, it might "swallow" more specific routes defined below it. Always place your specific routes above broader, wildcard routes.
- Method Mismatch: If you try to access a route via
POSTthat was defined withRoute::get, Laravel will throw a405 Method Not Allowederror. Ensure the HTTP verb matches the intended action. - Forgetting the leading slash: While Laravel is often forgiving, it's best practice to always include the leading slash in your URI strings (e.g.,
'/tasks', not'tasks'). - Caching: In production, we often cache routes for performance. If you define a new route and it doesn't seem to exist, you might need to clear your cache using
php artisan route:clear. You don't need to worry about this in local development, but it's a good "gotcha" to remember for later.
For those interested in how these concepts evolve into more complex architectures, I highly recommend reading about Laravel routing and controllers to see how we eventually move that logic out of the web.php file and into dedicated controllers to keep our code clean.
Recap
You've just learned the core of how Laravel handles incoming web traffic. We've defined basic GET routes, mapped them to string responses, and explored how the web.php file acts as the primary registry for your application's URLs. These basic building blocks allow you to start structuring the entry points for your Task Manager.
Up next: We'll move beyond static strings and learn how to make our routes dynamic by using Route Parameters.
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs — the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app — built with FilamentPHP so you can manage everything without touching the database.