PHP Logoslim/slim

Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. It's designed for speed, flexibility, and minimal overhead, focusing on common HTTP request methods like GET, POST, PUT, and DELETE. It achieves this by routing HTTP requests to controller functions based on URL patterns and HTTP methods.

Key features of Slim Framework include:

1. Routing: Slim provides a robust routing system that allows you to define application routes based on HTTP methods (GET, POST, PUT, DELETE, etc.) and URL patterns, including support for route parameters and named routes.
2. Middleware: It supports middleware, allowing you to intercept and process HTTP requests and responses at various stages of the application lifecycle (e.g., authentication, logging, CORS).
3. PSR-7 Compatibility: Slim is fully compatible with PSR-7 (HTTP message interfaces), which defines standard interfaces for representing HTTP requests and responses. This ensures interoperability with other PHP libraries.
4. Dependency Injection Container: By default, Slim uses Pimple as its dependency injection container, making it easy to manage and inject services into your application. This can be replaced with other PSR-11 compatible containers.
5. Error Handling: It provides mechanisms for graceful error handling and custom error pages.
6. Lightweight: Being a micro-framework, Slim has a small footprint, making it ideal for high-performance applications and APIs.

Slim is an excellent choice for developing RESTful APIs, small-to-medium-sized web applications, single-page applications (SPAs) backends, and microservices where a full-stack framework might be overkill.

Example Code

```php
<?php

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

use Slim\Factory\AppFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;

// 1. Create a Slim App instance
$app = AppFactory::create();

// 2. Add middleware (order matters!)
// Add the Body Parsing Middleware to parse JSON, form data, etc.
$app->addBodyParsingMiddleware();

// Add the Routing Middleware. It should be added after all application middleware
// but before the ErrorMiddleware.
$app->addRoutingMiddleware();

// Add Error Middleware (optional, but good for development)
// Set to true to display errors in browser
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

// 3. Define routes

// A basic GET route
$app->get('/', function (Request $request, Response $response, array $args) {
    $response->getBody()->write("Hello from Slim Framework!");
    return $response;
});

// A GET route with a parameter
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name']; // Get the 'name' parameter from the URL
    $response->getBody()->write("Hello, $name!");
    return $response;
});

// A POST route example for handling form submissions or API data
$app->post('/submit', function (Request $request, Response $response, array $args) {
    // Get parsed body data (e.g., from a form or JSON payload)
    $data = $request->getParsedBody();
    $username = $data['username'] ?? 'Anonymous'; // Default to 'Anonymous' if 'username' is not provided
    
    $response->getBody()->write("Thank you, $username, for your submission!");
    return $response->withHeader('Content-Type', 'text/plain')->withStatus(200);
});

// Example of grouping routes
$app->group('/users', function (RouteCollectorProxy $group) {
    $group->get('', function (Request $request, Response $response, array $args) {
        $response->getBody()->write("List of users");
        return $response;
    });

    $group->get('/{id}', function (Request $request, Response $response, array $args) {
        $id = $args['id'];
        $response->getBody()->write("Details for user ID: $id");
        return $response;
    });

    $group->post('', function (Request $request, Response $response, array $args) {
        $response->getBody()->write("Create a new user");
        return $response->withStatus(201);
    });
});

// 4. Run the Slim application
$app->run();

// To run this code:
// 1. Create a new directory for your project.
// 2. Open your terminal in that directory and run: `composer require slim/slim`
// 3. Create an `index.php` file and paste the code above into it.
// 4. Set up a web server (e.g., Apache, Nginx) to point to your project's `public` directory (or the directory containing `index.php`).
//    Alternatively, use PHP's built-in web server: `php -S localhost:8000` from your project root.
// 5. Access the routes in your browser or with a tool like Postman/cURL:
//    - `http://localhost:8000/` will output "Hello from Slim Framework!"
//    - `http://localhost:8000/hello/World` will output "Hello, World!"
//    - Make a POST request to `http://localhost:8000/submit` with a `username` field (e.g., in form-data or JSON body) to see the POST example.
//    - `http://localhost:8000/users` will output "List of users"
//    - `http://localhost:8000/users/123` will output "Details for user ID: 123"
```