Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Meet Slim

Slim is a micro-framework for PHP. A framework is a ready-made skeleton for an application, and "micro" means the skeleton holds only the essentials: it accepts the browser's request, finds the code for it and sends a response. Everything else — templates, a database, user login — is added with separate packages.

The course is written for Slim 4 and PHP 8.1 or newer.

What an application looks like

The smallest Slim application fits in one file:

// public/index.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();
 
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello, Slim!');
 
    return $response;
});
 
$app->run();

This code answers a request for the home page with the string "Hello, Slim!". For now it is enough to read it: in the next lesson you will create this file and run it.

What Slim includes and what it does not

Included in SlimAdded separately
Routes — linking an address to codePage templates — Twig
Request and response objectsDatabase — PDO, Eloquent, Doctrine
Middleware — layers around the handlerDependency container — PHP-DI
Error handlingSessions, user login, form validation

Packages are installed by Composer, the PHP dependency manager. Every lesson that needs a new package starts with the install command.

Slim and the big frameworks

In Laravel or Symfony the folder structure, database layer and templates are already chosen for you. Slim has no predefined folders and no console commands that generate files: you create every file by hand and decide where it goes.

That suits small sites and APIs — programming interfaces that return data as JSON. But a beginner can easily get lost, so this course builds the project structure step by step, and every piece of code says which file it belongs to.

What the course builds

The running example is a blog with posts:

  1. Chapter one: a single application in public/index.php — routes, the request and the response.
  2. Chapter two: the code is split into config/ and src/; action classes, the container and middleware appear.
  3. Chapter three: Twig pages, a database, forms, user login.
  4. Chapter four: errors and logging, an API, tests and deployment.

The ready-made skeleton

There is a ready-made project skeleton, the slim/slim-skeleton package. It comes with folders, a container and tests already set up. The course does not use it: an application assembled by hand is understood down to the last line.

Common mistake

Slim does nothing "by itself". In a big framework form data, the session or the database are available right away; here each of those features appears only after you install a package and wire it up in code. When something "does not work", first check whether it is connected at all.

Thanks for reading!

Donate to the site — we work hard for you.