Simple REST API - Slim Typing CST Test
Loading…
Simple REST API — Slim Code
Demonstrates a simple Slim app with routes for listing and creating items, using minimal setup.
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
// Sample in-memory data
$items = [];
$app->get('/items', function (Request $request, Response $response) use (&$items) {
$response->getBody()->write(json_encode($items));
return $response->withHeader('Content-Type', 'application/json');
});
$app->post('/items', function (Request $request, Response $response) use (&$items) {
$data = json_decode($request->getBody(), true);
$items[] = $data;
$response->getBody()->write(json_encode($data));
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();Slim Language Guide
Slim is a lightweight PHP micro-framework designed for quickly building simple yet powerful web applications and APIs. It focuses on minimalism, flexibility, and performance, giving developers full control over application architecture without imposing heavy conventions.
Primary Use Cases
- ▸RESTful API development
- ▸Single-page application (SPA) backends
- ▸Microservices
- ▸Prototyping lightweight web apps
- ▸Custom routing and middleware-driven applications
Notable Features
- ▸PSR-compliant HTTP message handling
- ▸Simple and powerful routing system
- ▸Middleware support for request/response customization
- ▸Error and exception handling with customizable error pages
- ▸Supports dependency injection and container integration
Origin & Creator
Created by Josh Lockhart in 2010, Slim was designed to be a fast, minimalistic framework for PHP developers who needed flexibility without unnecessary overhead.
Industrial Note
Slim is often chosen by startups, microservices developers, and API-first teams who want a minimalistic yet PSR-compliant framework without the weight of full-stack frameworks.