Defining Routes in Laravel
Register GET, POST, and resource routes in web.php and api.php.
Defining Routes in Laravel is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Basic Route
Define a GET route that returns a string response.
<?php
use Illuminate\Support\Facades\Route;
Route::get("/hello", function () {
return "Hello, Laravel!";
});HTTP Verb Methods
Laravel provides get, post, put, patch, delete, options route methods.
<?php
Route::post("/users", [UserController::class, "store"]);
Route::put("/users/{id}", [UserController::class, "update"]);
Route::delete("/users/{id}", [UserController::class, "destroy"]);Route Parameters
Capture URI segments with {param}. Optional segments use {param?}.
<?php
Route::get("/users/{id}", function (int $id) {
return "User ID: $id";
});
// Optional:
Route::get("/posts/{slug?}", function (?string $slug = null) { });Named Routes
Name routes for use in redirects and URL generation.
<?php
Route::get("/dashboard", [DashboardController::class, "index"])->name("dashboard");
// Elsewhere:
return redirect()->route("dashboard");
$url = route("dashboard");Route Groups
Group routes that share middleware, prefix, or namespace.
<?php
Route::middleware("auth")->prefix("admin")->group(function () {
Route::get("/dashboard", [AdminController::class, "index"]);
Route::get("/users", [AdminController::class, "users"]);
});Resource Routes
Route::resource() registers 7 RESTful routes (index, create, store, show, edit, update, destroy) for a controller in one line.
<?php
Route::resource("posts", PostController::class);
// GET /posts, GET /posts/create, POST /posts, GET /posts/{post}
// GET /posts/{post}/edit, PUT/PATCH /posts/{post}, DELETE /posts/{post}API Resource Routes
Route::apiResource() registers 5 routes (skipping create and edit which return forms).
<?php
Route::apiResource("users", UserController::class);Route Model Binding
Type-hint a model in a route closure or controller method — Laravel automatically fetches the model by primary key.
<?php
Route::get("/users/{user}", function (User $user) {
return $user; // Laravel fetches User::findOrFail($user)
});Route Constraints
Constrain route parameter formats with regular expressions.
<?php
Route::get("/users/{id}", handler)->where("id", "[0-9]+");
Route::get("/users/{slug}", handler)->where("slug", "[a-z-]+");any() and match()
Route::any() responds to any HTTP verb. Route::match() responds to a list of verbs.
<?php
Route::match(["get", "post"], "/contact", ContactController::class);Viewing Registered Routes
List all registered routes with their methods, names, and actions.
$ php artisan route:listSummary
Laravel routes live in routes/web.php and routes/api.php. Use resource routes for CRUD, route model binding for automatic model injection, and named routes for URL generation.
Quick Check
Which command registers 7 RESTful routes at once?
Frequently asked questions
Is the “Defining Routes in Laravel” lesson free?
Yes — the full text of “Defining Routes in Laravel” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining Routes in Laravel”?
Register GET, POST, and resource routes in web.php and api.php. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Defining Routes in Laravel” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Laravel Project Structure
- Defining Routes in Laravel
- Controllers and Actions
- Blade Templates and Views