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.
Quick Explain
- ▸Slim is a micro-framework that provides just the essentials for routing, middleware, and request/response handling.
- ▸It follows PSR-7, PSR-15, and PSR-17 standards for HTTP message interfaces and middleware interoperability.
- ▸Includes routing, dependency injection, error handling, and middleware support out of the box.
- ▸Ideal for building RESTful APIs, single-page applications (SPA) backends, and small-to-medium web apps.
- ▸Its simplicity allows full customization and integration with other components or frameworks.
Core Features
- ▸Minimalist architecture with focus on HTTP handling
- ▸Flexible routing with route placeholders and named routes
- ▸Middleware stack for request/response modification
- ▸Integration with any templating engine or ORM
- ▸PSR-compliant HTTP messages and middleware interoperability
Learning Path
- ▸Learn PHP and object-oriented programming
- ▸Understand HTTP request/response cycle
- ▸Learn PSR-7 and PSR-15 standards
- ▸Practice building RESTful APIs with Slim
- ▸Explore middleware, DI, and route grouping
Practical Examples
- ▸Create a REST API for a blog application
- ▸Build a JSON API for a mobile app
- ▸Implement JWT-based authentication middleware
- ▸Develop a lightweight admin dashboard backend
- ▸Set up logging and error handling for microservices
Comparisons
- ▸Slim vs Laravel - micro vs full-stack
- ▸Slim vs Symfony - lightweight vs enterprise features
- ▸Slim vs Lumen - minimalism vs Laravel micro-framework
- ▸Slim vs CodeIgniter - routing-focused vs lightweight full MVC
- ▸Slim vs Zend Expressive - PSR middleware-centric frameworks
Strengths
- ▸Lightweight and fast
- ▸Highly flexible and extensible
- ▸PSR compliance ensures interoperability with other PHP packages
- ▸Easy to learn and start with
- ▸Well-suited for APIs and microservices
Limitations
- ▸Not a full-stack framework, so you must integrate ORM, templating, and auth yourself
- ▸Less opinionated, so requires architectural decisions from the developer
- ▸Smaller community compared to Laravel or Symfony
- ▸May need more boilerplate for large-scale applications
- ▸Limited built-in features compared to full-stack frameworks
When NOT to Use
- ▸If you need full-stack features like ORM, templating, and auth prebuilt
- ▸When rapid prototyping with conventions is preferred
- ▸For complex enterprise apps requiring opinionated architecture
- ▸If community support and learning resources are a priority
- ▸When tight integration with front-end tooling is needed out-of-the-box
Cheat Sheet
- ▸$app = AppFactory::create(); // initialize Slim app
- ▸$app->get('/route', function($request, $response) {...}); // define route
- ▸$app->add(MiddlewareClass::class); // add middleware
- ▸$response->getBody()->write('Hello'); // write response
- ▸$app->run(); // run the application
FAQ
- ▸Is Slim still maintained? -> Yes, active development
- ▸Does Slim support PHP 8? -> Yes
- ▸Can Slim handle REST APIs? -> Yes, designed for APIs
- ▸Does Slim have built-in ORM? -> No, integrate separately
- ▸Is Slim suitable for microservices? -> Yes, highly suitable
30-Day Skill Plan
- ▸Week 1: Simple route-based API
- ▸Week 2: Add middleware and error handling
- ▸Week 3: Integrate database via ORM
- ▸Week 4: Implement authentication and caching
- ▸Week 5: Test, optimize, and deploy API services
Final Summary
- ▸Slim is a minimalist PHP micro-framework for web applications and APIs.
- ▸It emphasizes flexibility, PSR compliance, and simplicity.
- ▸Ideal for RESTful APIs, microservices, and lightweight apps.
- ▸Highly extensible via middleware and dependency injection.
- ▸Perfect for developers who want control without full-stack overhead.
Project Structure
- ▸public/ - web server entry point
- ▸src/ - application code (Controllers, Services, Middleware)
- ▸config/ - configuration files
- ▸logs/ - error and access logs
- ▸vendor/ - Composer dependencies
Monetization
- ▸Develop SaaS APIs using Slim
- ▸Custom client backends
- ▸Microservices for enterprise solutions
- ▸REST APIs for mobile apps
- ▸Consulting and API architecture services
Productivity Tips
- ▸Use middleware for reusable request/response logic
- ▸Keep routes and services modular
- ▸Integrate caching early for API-heavy apps
- ▸Use DI container for clean dependency management
- ▸Test APIs incrementally to catch issues early
Basic Concepts
- ▸App - the main Slim application instance
- ▸Route - defines a path and the callback/action
- ▸Middleware - intercepts request/response for pre/post-processing
- ▸Request - encapsulates HTTP request data
- ▸Response - encapsulates HTTP response data
Official Docs
- ▸https://www.slimframework.com/docs/
- ▸Slim GitHub repository
- ▸Slim API documentation