Practical Web Data Patterns
Route Matches
Match a path to a handler label.
route-match
A small router maps a path to a handler. The lesson uses fixed path strings instead of a live server request.
Route Matches
route_match.php
Replay: real traced execution (multi-file project)
<?php
$path = "/users";
$handler = "not_found";
if ($path === "/users") {
$handler = "users_index";
} elseif ($path === "/health") {
$handler = "health_check";
}
echo "path=" . $path . "\n";
echo "handler=" . $handler . "\n";
<?php
$path = "/health";
$handler = "not_found";
if ($path === "/users") {
$handler = "users_index";
} elseif ($path === "/health") {
$handler = "health_check";
}
echo "path=" . $path . "\n";
echo "handler=" . $handler . "\n";
<?php
$path = "/missing";
$handler = "not_found";
if ($path === "/users") {
$handler = "users_index";
} elseif ($path === "/health") {
$handler = "health_check";
}
echo "path=" . $path . "\n";
echo "handler=" . $handler . "\n";
$path ← /users, $handler ← not_found
1<?php2$path→ /users = "/users"; //@path="/health", "/missing"3$handler→ not_found = "not_found";$handler ← users_index
5if ($path/users === "/users") {6 $handler→ users_index = "users_index";7} elseif ($path === "/health") {echo "path=" . $path . " ";
11echo "path=" . $path/users . "\n";12echo "handler=" . $handlerusers_index . "\n";outputpath=/users handler=users_index
$path ← /health, $handler ← not_found
1<?php2$path→ /health = "/health";3$handler→ not_found = "not_found";45if ($path === "/users") {6 $handler = "users_index";7} elseif ($path === "/health") {8 $handler→ health_check = "health_check";9}1011echo "path=" . $path/health . "\n";12echo "handler=" . $handlerhealth_check . "\n";outputpath=/health handler=health_check
$path ← /missing, $handler ← not_found
1<?php2$path→ /missing = "/missing";3$handler→ not_found = "not_found";45if ($path === "/users") {6 $handler = "users_index";7} elseif ($path === "/health") {8 $handler = "health_check";9}1011echo "path=" . $path/missing . "\n";12echo "handler=" . $handlernot_found . "\n";outputpath=/missing handler=not_found