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.
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
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.