Skip to content

Guide · Open source

How to use Slim: install, write routes and run a PHP app

Updated · written with AI from the project's own documentation and checked against our catalogue data

Slim is a PHP micro-framework for building web applications and APIs. This guide walks you through installing it with Composer, choosing a PSR-7 implementation, writing a small app with routes, and running it locally. By the end you will have a working two-route app and know where to go next.

Slimslimphp/Slim · ★ 12.3k · MIT

Quick start

  1. 1Check your environment. Confirm PHP 7.4 or newer and Composer are installed.
  2. 2Install Slim. Run composer require slim/slim in your project folder.
  3. 3Install a PSR-7 implementation. Install one of the supported implementations, such as slim/psr7, so AppFactory can detect it.
  4. 4Create public/index.php. Add the app file that loads the autoloader, creates the app and defines routes.
  5. 5Add routes. Use methods like get to map paths such as /hello/{name} to PHP functions.
  6. 6Start the built-in server. Run php -S localhost:8000 -t public and open the address in your browser.

What you need before installing Slim

Slim requires PHP 7.4 or newer and Composer, the PHP dependency manager. Slim is installed as a package through Composer, so you will also need a way to run PHP from the command line.

  • PHP 7.4 or newer
  • Composer

Install Slim with Composer

The README recommends installing Slim with Composer. Run the following command in your project folder. It installs Slim and all of its required dependencies.

composer require slim/slim

Choose a PSR-7 implementation

Slim needs a PSR-7 HTTP message implementation to handle requests and responses. For automatic detection with AppFactory::create() and App::run(), you install one of the supported implementations. The README lists several options, each with its own install command.

Slim-Psr7 is the Slim Framework's own implementation. Other options include httpsoft/http-message with httpsoft/http-server-request, Nyholm/psr7 with Nyholm/psr7-server, Guzzle's psr7 package, and laminas-diactoros. The README describes httpsoft as the fastest, strictest and most lightweight option, and Nyholm as having performance almost the same as httpsoft.

  • Slim-Psr7: composer require slim/psr7
  • httpsoft: composer require httpsoft/http-message httpsoft/http-server-request
  • Nyholm: composer require nyholm/psr7 nyholm/psr7-server
  • Guzzle: composer require guzzlehttp/psr7
  • Laminas: composer require laminas/laminas-diactoros
composer require slim/psr7

Create your first app in public/index.php

Create the file public/index.php. It loads Composer's autoloader, creates the app, adds error middleware, and defines two routes. The first route serves a link to the second. The second route reads a name from the URL and writes it into the response.

The error middleware call takes three boolean arguments in the example. Routes are defined with methods such as get, and the {name} part of a path is passed to your function in the $args array.

<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/../vendor/autoload.php'; // Instantiate App $app = AppFactory::create(); // Add error middleware $app->addErrorMiddleware(true, true, true); // Add routes $app->get('/', function (Request $request, Response $response) { $response->getBody()->write('<a href="/hello/world">Try /hello/world</a>'); return $response; }); $app->get('/hello/{name}', function (Request $request, Response $response, $args) { $name = $args['name']; $response->getBody()->write("Hello, $name"); return $response; }); $app->run();

Run the app locally

The README suggests testing with PHP's built-in server. Run the command below from your project root, then open the address in a browser. Visiting /hello/world should display "Hello, world".

For production, the README points to the documentation on configuring a web server rather than the built-in server.

  • Open http://localhost:8000/hello/world to see the greeting
php -S localhost:8000 -t public

Run the test suite (for contributors)

If you want to work on Slim itself, the README says to clone the repository, install development dependencies, and run the test script. This step is not needed to use Slim in your own project.

git clone https://github.com/slimphp/Slim composer install composer test

What people use it for

  • Building a small JSON or HTTP API with a handful of routes
  • Writing a simple web endpoint or webhook receiver in PHP
  • Learning how HTTP routing and middleware work in PHP
  • Adding a lightweight PHP service where a full-stack framework would be more than you need

Frequently asked questions

Is Slim free to use?

Yes. Slim is licensed under the MIT license, as stated in the README. See the LICENSE.md file in the repository for the full terms.

What do I need to install Slim?

The README says Slim requires PHP 7.4 or newer. You install it with Composer using composer require slim/slim.

Why do I need a PSR-7 implementation?

Slim handles the request and response flow but needs a PSR-7 HTTP message library to create request and response objects. For AppFactory::create() and App::run() to work without manual setup, you install one of the supported implementations.

Why does my app fail when I call AppFactory::create()?

The README says auto-detection only works when one of the supported PSR-7 implementations is installed. Install one, such as composer require slim/psr7, and try again. The README does not list other specific causes.

Is Slim a good alternative to Laravel or Symfony?

Slim is a micro-framework that leaves many choices to you, including ORM and admin tooling. Laravel, Symfony, Lumen and Mezzio are the comparable projects listed for this page. Pick Slim if you want a small core; pick a full framework if you want built-in batteries.

Can I use Slim on an older PHP version?

The README states that PHP 7.4 or newer is required, so Slim is not intended for projects that must run on older versions.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

Slim suits PHP developers who want a small framework for simple web apps and JSON or HTTP APIs, and who are happy to choose their own PSR-7 library. If you need a full-stack framework with a built-in ORM and admin panels, or you must support PHP older than 7.4, look at Laravel, Symfony, Lumen or Mezzio instead.

Install options & downloads