CodeBlanks Logo Icon CodeBlanks

CRUD endpoints

PHP course · Advanced · Lesson 57/92

Wire GET, POST, PUT and DELETE endpoints to PDO.

The dispatch method

The dispatch method takes the incoming request and searches the registered routes for a match. Its signature is dispatch(string $method, string $path). It walks the $this->routes array with a foreach and compares each route's method and path fields against the incoming values.

When a match is found it calls the handler and returns its value: return ($route['handler'])();. If nothing matches it returns an error response.

Calling the handler

The PHP way to call a closure or a function name is the ($handler)() syntax. If $handler is a closure it is called directly; if it is a string, the function of that name is called.

This flexibility lets both anonymous functions and names like "handleGet" work with the same dispatch logic. The handler type does not complicate dispatch.

Exercise Add the dispatch method

Add a dispatch method to the Router class. Its signature is public function dispatch(string $method, string $path). The method walks $this->routes with a foreach and compares each route's method and path fields against the incoming values using strict ===. On a match it calls the handler with return ($route['handler'])();. With no match it returns return "404 Not Found";.

Show example solution
Write public function dispatch(string $method, string $path). Loop with foreach ($this->routes as $route); compare with if ($route["method"] === $method && $route["path"] === $path). On match return ($route["handler"])(); otherwise return "404 Not Found";.
Example solution:
<?php
// REST API project: Router + addRoute + dispatch

class Router {
    public array $routes = [];
    public function addRoute(string $method, string $path, $handler): void {
    $this->routes[] = ["method" => $method, "path" => $path, "handler" => $handler];
}
    public function dispatch(string $method, string $path) {
    foreach ($this->routes as $route) {
        if ($route["method"] === $method && $route["path"] === $path) {
            return ($route["handler"])();
        }
    }
    return "404 Not Found";
}
}

// Demo:
$router = new Router();
$router->addRoute("GET", "/tasks", function() { return "Task list"; });
echo $router->dispatch("GET", "/tasks");

Test yourself

  1. Match the HTTP methods

    GET
    List a resource
    POST
    Create a resource
    PUT
    Update a resource
    DELETE
    Delete a resource
  2. What should dispatch return when no route matches?

    • An empty string
    • A 404 error response
    • A 200 success response
    • An empty array
    Show answer

    A 404 error response — Correct. No match means the resource was not found, so 404 is returned.

  3. Complete the match condition

    if ($route["method"] === $method && ____) { return ($route["handler"])(); }
    Show answer
    if ($route["method"] === $method && $route["path"] === $path) { return ($route["handler"])(); }

All lessons in Advanced