Lightweight PHP framework in 2026: Slim vs Flight vs Leaf vs plain PHP

Summary

A side-by-side build of the same tiny JSON API in Slim, Flight, Leaf, and plain PHP with FastRoute shows that the main tradeoff is not speed but how many packages, conventions, and standards each option brings. Slim installs 11 packages and loads 79 vendor files per request because it builds on PSR-7 and PSR-15 interfaces, but that buys interchangeable middleware, containers, and request/response implementations, making it the best default for maintainable APIs. Flight ships as one dependency-free package with the lowest overhead, though its custom before/after middleware and positional route parameters mean Slim middleware will not plug in, and JSON APIs should map both notFound and methodNotFound to avoid HTML 405 responses. Leaf 5 requires PHP 8.2, uses global helper functions, and rewrote its router so raw regex routes no longer match, while plain FastRoute plus a few lines of PHP handled 200, 401, 404, and 405 correctly with only 12 vendor files but leaves middleware and error handling to you. One practical gotcha is that when running under php -S you should pass -t with the directory containing index.php, because Flight and Leaf otherwise treat part of the request path as SCRIPT_NAME and return 404s for every route.

If you need routing, a bit of middleware and JSON responses in PHP, three lightweight PHP frameworks are still shipping releases at the end of 2026: Slim, Flight and Leaf. The real difference between them isn't speed. It's how much they bring with them and whose conventions you sign up for. Slim is built on the PHP-FIG standards and installs eleven packages to get there. Flight is a single package with no dependencies at all. Leaf sits in between with its own helper-function style. And for a handful of endpoints, plain PHP with FastRoute (the router Slim itself uses) is a fair fourth option.

To compare them on equal terms we built the same tiny API in each one: GET /users/{id} returning JSON, a numeric-only id, an API-key check written as middleware, and a JSON body for 404s. Then we hit each version with curl on PHP 8.3.35's built-in server.

What each framework installs

  Slim Flight Leaf FastRoute only
Latest stable 4.16.1 (2026-12-04) 3.19.5 (2026-10-27) 5.1.0 (2026-12-04) 1.4.0 (2026-12-03)
PHP required 7.4 to 8.6 7.4+ 8.2+ 7.3+
Packages installed 11 1 6 1
vendor/ on disk 1,132 KB 272 KB 672 KB 120 KB
Vendor files loaded per request 79 14 20 12
Peak memory per request 1.29 MB 0.96 MB 1.20 MB 0.51 MB
Packagist installs 60.3 million 145 thousand 60 thousand 110 million

The versions, PHP constraints and dependency trees come from each package's Packagist metadata as of December 5, 2026; Slim's count includes slim/psr7, because Slim needs some PSR-7 implementation. Files loaded and peak memory are for one simulated GET /users/42 in the PHP CLI, counting only files under vendor/. We installed from the packages' dist archives with a minimal PSR-4 autoloader rather than Composer's generated one, so a normal Composer install adds a few autoloader files to every column.

None of these numbers decides anything on its own. Every option stays under a megabyte and a half per request. What they show is shape. Slim's 79 files break down as 38 from Slim itself, 14 from slim/psr7, 11 from FastRoute, 13 from the PSR interface packages and 3 from two small helpers and the autoloader. More layers and more classes is what building on shared interfaces costs, compared with one hand-rolled request object.

Slim: built on the PSR standards

Slim's installation guide has you run composer require slim/slim:"4.*" and then pick a PSR-7 implementation yourself: Slim's own slim/psr7, Nyholm, Guzzle or Laminas Diactoros. Middleware follows PSR-15, the standard process(request, handler) interface, and a closure with the same shape works too:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Slim\Factory\AppFactory;

require __DIR__ . '/vendor/autoload.php';

$app = AppFactory::create();

// PSR-15 style middleware: reject requests without the API key
$app->add(function (Request $request, Handler $handler): Response {
    if ($request->getHeaderLine('X-Api-Key') !== 'secret') {
        $response = new \Slim\Psr7\Response(401);
        $response->getBody()->write(json_encode(['error' => 'Unauthorized']));
        return $response->withHeader('Content-Type', 'application/json');
    }
    return $handler->handle($request);
});

$app->get('/users/{id:[0-9]+}', function (Request $request, Response $response, array $args) {
    $response->getBody()->write(json_encode(['id' => (int) $args['id'], 'name' => 'user' . $args['id']]));
    return $response->withHeader('Content-Type', 'application/json');
});

$app->addRoutingMiddleware();
$app->addErrorMiddleware(false, false, false);
$app->run();

Two things in Slim's docs showed up in the run. Middleware runs last in, first out, so the error middleware is added last to make it the outermost layer. And the error handler picks its output format from the request's Accept header. With Accept: application/json, /users/abc came back as {"message": "404 Not Found"}; without that header, the same request returned an HTML error page. If your API clients don't send Accept, call forceContentType('application/json') on the error handler, as the error handling docs show. A POST to the GET route got a correct 405 with an Allow: GET header without any extra code.

What you get for the extra packages is interchangeability. Any PSR-15 middleware on Packagist (CORS, JWT checks, rate limiting) plugs straight in, Slim works with any PSR-11 container such as PHP-DI rather than shipping its own, and you can swap the PSR-7 implementation without touching your handlers. Slim 4.16 (November 29, 2026) also added PHP 8.6 to its supported versions; 4.15.3 stopped at 8.5.

Flight: one package, no dependencies

Flight's documentation puts it plainly: "Flight's core is completely dependency-free—no polyfills, no external packages, not even PSR interfaces." composer require flightphp/core installs exactly one package, and it still supports PHP 7.4.

<?php
require __DIR__ . '/vendor/autoload.php';

class ApiKeyMiddleware
{
    public function before(array $params): void
    {
        if (Flight::request()->getHeader('X-Api-Key') !== 'secret') {
            Flight::jsonHalt(['error' => 'Unauthorized'], 401);
        }
    }
}

Flight::group('', function () {
    Flight::route('GET /users/@id:[0-9]+', function (string $id) {
        Flight::json(['id' => (int) $id, 'name' => 'user' . $id]);
    });
}, [ApiKeyMiddleware::class]);

Flight::map('notFound', function () {
    Flight::jsonHalt(['error' => 'Not Found'], 404);
});

Flight::start();

A few things differ from Slim. Route parameters are passed to the callback by position, not by name, so swapping the argument order swaps the values. Middleware is a class with before() and after() methods rather than PSR-15, which means middleware written for Slim won't plug in. The routing docs also warn that returning a value from a route handler can fall through to a 404; echo or call Flight::json() instead.

The notFound mapping covers only 404s. Our POST to /users/42 got a 405 back as text/html with the body Method Not Allowed. Allowed Methods are: GET, HEAD, OPTIONS. Flight's Engine lists methodNotFound as a separate method you can override with Flight::map(), so a JSON API should map both. You also don't have to use the static calls: Flight::app() returns the engine instance, and the routing docs call the router object the preferred way to define routes and groups.

Leaf 5: helper functions and a rewritten router

Leaf 5.0 (August 2026) replaced the router. According to the release notes, routes now compile at registration, inline constraints such as {id:[0-9]+} and optional parameters work, and raw regex routes like /movies/(\d+) no longer match, which breaks Leaf 4 route files that used them. Leaf 5 requires PHP 8.2, and 5.1.0 added a worker mode for FrankenPHP.

<?php
require __DIR__ . '/vendor/autoload.php';

// Leaf middleware is a plain function; this one runs for every request
app()->use(function () {
    if (request()->headers('X-Api-Key') !== 'secret') {
        response()->exit(['error' => 'Unauthorized'], 401);
    }
});

app()->get('/users/{id:[0-9]+}', function ($id) {
    response()->json(['id' => (int) $id, 'name' => 'user' . $id]);
});

app()->set404(function () {
    response()->json(['error' => 'Not Found'], 404);
});

app()->run();

The style is global helpers: app(), request() and response(). The middleware docs describe middleware as "just functions", and app()->use() runs one for every request. The six packages leafs/leaf pulled in are all Leaf's own. One difference from the other three: a POST to the GET route returned 404, not 405, so clients can't tell a wrong method from a wrong URL. Leaf's draw is what sits around the core: separate modules for auth, database and the like, and a Leaf MVC skeleton if the app grows.

Or no framework: FastRoute and a few lines of PHP

FastRoute is the router inside Slim, and on its own it's one package. The FastRoute README says to strip the query string and rawurldecode() the path before dispatching, which the version below does:

<?php
require __DIR__ . '/vendor/autoload.php';

function json_out(int $status, array $data): void
{
    http_response_code($status);
    header('Content-Type: application/json');
    echo json_encode($data);
}

if (($_SERVER['HTTP_X_API_KEY'] ?? '') !== 'secret') {
    json_out(401, ['error' => 'Unauthorized']);
    return;
}

$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
    $r->addRoute('GET', '/users/{id:[0-9]+}', function (array $vars) {
        json_out(200, ['id' => (int) $vars['id'], 'name' => 'user' . $vars['id']]);
    });
});

$path = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$route = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], $path);

match ($route[0]) {
    FastRoute\Dispatcher::FOUND => $route[1]($route[2]),
    FastRoute\Dispatcher::METHOD_NOT_ALLOWED => json_out(405, ['error' => 'Method Not Allowed']),
    default => json_out(404, ['error' => 'Not Found']),
};

This version returned the right 200, 401, 404 and 405 responses and loaded 12 vendor files using 0.51 MB. What it leaves out is everything around the router: no request or response objects, no middleware pipeline (the API-key check is just an if before dispatch) and no error handler. For a webhook receiver or a five-endpoint internal API that's fine. Once you find yourself writing a middleware stack, you're rebuilding Slim.

Why every route 404s under php -S

Our first run started each app with php -S 127.0.0.1:8092 index.php, passing the router script but no -t document root. Slim and plain FastRoute worked. Flight and Leaf returned 404 for every request, including /users/42. Both strip a base path taken from $_SERVER['SCRIPT_NAME'] so they can run from a subdirectory. A probe script that only echoes SCRIPT_NAME and REQUEST_URI shows what the built-in server puts there:

# server started with: php -S 127.0.0.1:8098 probe/index.php   (no -t)
$ curl -s http://127.0.0.1:8098/users/42
SCRIPT_NAME=/users/42
REQUEST_URI=/users/42

# server started with: php -S 127.0.0.1:8099 -t probe probe/index.php
$ curl -s http://127.0.0.1:8099/users/42
SCRIPT_NAME=/index.php
REQUEST_URI=/users/42

Without -t, the framework treats /users as the base path and tries to route /42. Pointing -t at the folder that contains index.php fixed both. The same thing can happen behind a web server whose rewrite rules leave SCRIPT_NAME pointing somewhere unexpected, so it's worth checking before you start debugging routes.

Which lightweight PHP framework to pick

Our default is Slim for anything more than one person will maintain. The extra packages buy standard interfaces: middleware from other projects works, the request and response types are the same ones other PSR-7 libraries use, and with 60 million installs it's the most widely used of the three.

  • Pick Flight for a single-file tool, a small site on older shared hosting, or when "one dependency" is a hard requirement. Map both notFound and methodNotFound if it serves JSON.
  • Pick Leaf if you like Laravel-style helpers and expect to use its auth and database modules. You need PHP 8.2, and if you're upgrading from Leaf 4, rewrite raw regex routes first.
  • Skip the framework and use FastRoute directly when the app is a handful of endpoints and you're happy to own the error handling.
PHP SLIM FRAMEWORK FLIGHT PHP LEAF PHP FASTROUTE PSR-15 MICRO FRAMEWORK

  RELATED

  COMMENTS

0

No comment for this article.