Installation & Setup

Before you can build anything with Slim, you need Composer — PHP's dependency manager — and then you need to pull Slim itself into a project using Composer. This page walks through both steps, the project structure Composer creates, and how to run your app locally.

Note: This tutorial site itself is a plain PHP site with no Composer or Slim installed. Every code sample below is a correct, static example meant to be copied into your own separate Slim project — not something that runs live on this page.

Step 1: Make Sure Composer Is Installed

If you already installed Composer while learning Core PHP, you can skip this step. Otherwise, download and install it from getcomposer.org — on Windows it comes as a simple installer, and on Linux/macOS it's a couple of shell commands. Once installed, verify it from a terminal:

composer --version

Step 2: Create a New Project and Require Slim

Create a new folder for your project, then use Composer to require Slim 4. Slim 4 does not bundle a PSR-7 implementation the way Slim 3 did — you must explicitly choose one, so we'll add slim/psr7 alongside it:

composer require slim/slim:"4.*"
composer require slim/psr7
Why a separate PSR-7 package? PSR-7 is just an interface standard for HTTP messages. Slim 4 lets you plug in any compatible implementation (Slim PSR-7, Nyholm, Guzzle PSR-7, etc.) instead of forcing one on you.

Resulting Project Structure

After Composer finishes, your project will look roughly like this:

my-slim-app/
├── composer.json
├── composer.lock
├── vendor/              // all downloaded packages, autoloader lives here
└── public/
    └── index.php        // the front controller — every request enters here

composer.json records which packages your project depends on (Slim, PSR-7, and anything else you add later). The vendor/ folder is generated by Composer and should never be edited by hand. public/index.php is the single entry point — the front controller — that every incoming HTTP request should be routed through.

Step 3: Route All Requests Through public/index.php

A Slim app needs every request — no matter the URL path — to reach public/index.php so Slim's router can decide what to do with it. How you achieve that depends on your environment:

Apache (.htaccess rewrite)

Place an .htaccess file inside the public/ folder with a rewrite rule like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

This tells Apache: if the requested file doesn't physically exist, hand the request to index.php instead of returning a 404.

PHP's Built-in Server (for local development)

For quick local testing you don't need Apache or Nginx at all. PHP ships with a built-in development server — start it pointed at the public/ folder:

php -S localhost:8080 -t public

Visit http://localhost:8080 in a browser and requests will be served through public/index.php automatically.

Next steps: Now that Slim is installed and your server is configured, head to Your First App to write the actual bootstrap code.