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.
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."
A basic route definition consists of three parts:
Route::get, Route::post, Route::put, or Route::delete. For simply viewing pages, we use GET./tasks or /about).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.
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.
To get comfortable with the syntax, let's expand our project's routing:
routes/web.php./about that returns the string "This is a simple Task Manager built with Laravel."/contact that returns "Email us at support@taskmanager.test".Even experienced developers trip up on routing occasionally. Here are the most common issues:
POST that was defined with Route::get, Laravel will throw a 405 Method Not Allowed error. Ensure the HTTP verb matches the intended action.'/tasks', not 'tasks').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.
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.
Learn how to use Laravel route parameters to build dynamic, flexible URLs. Master required segments, optional parameters, and regex constraints today.
Read moreLearn how to create a TasksController and define a route for your Task Manager, moving from simple closures to a scalable MVC structure.
Defining Basic Web Routes
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation