← Back to slimphp/Slim

How to Deploy & Use slimphp/Slim

Slim Framework Deployment and Usage Guide

Prerequisites

  • PHP 7.4 or newer - Slim Framework requires PHP 7.4 or newer to run
  • Composer - Dependency manager for PHP (https://getcomposer.org/)
  • Web server (for production) - Apache, Nginx, or similar
  • PSR-7 Implementation - Choose one of the following:
    • slim/psr7 (Slim's official implementation)
    • httpsoft/http-message & httpsoft/http-server-request (fastest)
    • nyholm/psr7 & nyholm/psr7-server (high performance)
    • guzzlehttp/psr7 (Guzzle implementation)
    • laminas/laminas-diactoros (Laminas implementation)

Installation

Using Composer (Recommended)

$ composer require slim/slim

This will install Slim and all required dependencies.

Optional: Install Slim-Http Decorators

$ composer require slim/http

The Slim-Http library provides decorators for PSR-7 implementations and is recommended for use with Slim Framework.

Manual Installation

  1. Clone the repository:
$ git clone https://github.com/slimphp/Slim.git
$ cd Slim
  1. Install dependencies:
$ composer install

Configuration

PSR-7 Implementation Setup

Choose and install one of the PSR-7 implementations mentioned in Prerequisites. For example:

# Using Slim's official implementation
$ composer require slim/psr7

# Or using HttpSoft implementation
$ composer require httpsoft/http-message httpsoft/http-server-request

Slim-Http Decorators (Optional)

If you install Slim-Http and want to disable automatic decorator detection:

<?php

use Slim\Factory\AppFactory;
use Slim\Factory\ServerRequestCreatorFactory;

AppFactory::setSlimHttpDecoratorsAutomaticDetection(false);
ServerRequestCreatorFactory::setSlimHttpDecoratorsAutomaticDetection(false);

$app = AppFactory::create();

Environment Variables

Slim Framework doesn't require specific environment variables by default, but you may need to configure:

  • BASE_PATH - For applications running in subdirectories
  • CACHE_FILE - Path to fast route cache file (set to null to disable caching)

Build & Run

Hello World Application

Create a file public/index.php:

<?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();

Running Locally

Development Server

$ php -S localhost:8000 -t public

Production Server

For production, configure your web server to point to the public directory and ensure URL rewriting is enabled.

Testing

To execute the test suite:

$ composer test

Deployment

Platform Recommendations

Slim Framework is framework-agnostic and can be deployed on any PHP-compatible platform:

Shared Hosting

  • Upload files to your hosting account
  • Ensure PHP 7.4+ is available
  • Configure .htaccess for URL rewriting if needed

VPS/Dedicated Server

  • Install PHP 7.4+ and Composer
  • Configure Apache or Nginx with URL rewriting
  • Set appropriate file permissions

Cloud Platforms

  • Platform.sh - Native PHP support with automatic scaling
  • Heroku - Use PHP buildpack
  • AWS Elastic Beanstalk - PHP environment support
  • DigitalOcean App Platform - PHP runtime support

Container Deployment

FROM php:8.0-apache
COPY . /var/www/html
RUN docker-php-ext-install pdo pdo_mysql
RUN composer install --no-dev --optimize-autoloader

Production Considerations

  1. Enable Caching - Set $cacheFile in RouteCollector for performance
  2. Disable Debug Mode - Set error middleware to false in production
  3. Optimize Autoloader - Run composer dump-autoload --optimize
  4. Set Proper Permissions - Ensure web server can read/write cache files

Troubleshooting

Common Issues

1. "Class not found" Errors

Problem: PSR-7 implementation classes not found Solution: Ensure you've installed a PSR-7 implementation and it's properly autoloaded

# Check if PSR-7 implementation is installed
$ composer show | grep psr7

2. Routing Not Working

Problem: Routes not being matched correctly Solution: Verify URL rewriting is configured correctly

For Apache, ensure .htaccess contains:

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

3. Error Middleware Not Working

Problem: Error details not being displayed Solution: Check error middleware configuration

$app->addErrorMiddleware(
    $displayErrorDetails = true,  // Set to false in production
    $logErrors = true,
    $logErrorDetails = true
);

4. Performance Issues

Problem: Slow response times Solution: Enable route caching and optimize autoloader

// In RouteCollector
protected ?string $cacheFile = __DIR__ . '/../cache/routes.cache';

5. Dependency Conflicts

Problem: Composer dependency conflicts Solution: Check compatibility between Slim and your PSR-7 implementation

# Check for conflicts
$ composer why-not slim/psr7

Debug Mode

Enable debug mode during development to get detailed error information:

$app->addErrorMiddleware(true, true, true);

Remember to disable debug mode in production by setting the first parameter to false.