Web Servers

It is typical to use the front-controller pattern to funnel appropriate HTTPrequests received by your web server to a single PHP file. The instructionsbelow explain how to tell your web server to send HTTP requests to your PHPfront-controller file.

PHP built-in server

Run the following command in terminal to start localhost web server,assuming ./public/ is public-accessible directory with index.php file:

  1. php -S localhost:8888 -t public public/index.php

If you are not using index.php as your entry point then change appropriately.

Apache configuration

Ensure your .htaccess and index.php files are in the samepublic-accessible directory. The .htaccess file should contain this code:

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

This .htaccess file requires URL rewriting. Make sure to enable Apache’s mod_rewrite module and your virtual host is configured with the AllowOverride option so that the .htaccess rewrite rules can be used:

  1. AllowOverride All

Nginx configuration

This is an example Nginx virtual host configuration for the domain example.com.It listens for inbound HTTP connections on port 80. It assumes a PHP-FPM serveris running on port 9000. You should update the server_name, error_log,access_log, and root directives with your own values. The root directiveis the path to your application’s public document root directory; your Slim app’sindex.php front-controller file should be in this directory.

  1. server {
  2. listen 80;
  3. server_name example.com;
  4. index index.php;
  5. error_log /path/to/example.error.log;
  6. access_log /path/to/example.access.log;
  7. root /path/to/public;
  8. location / {
  9. try_files $uri /index.php$is_args$args;
  10. }
  11. location ~ \.php {
  12. try_files $uri =404;
  13. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  14. include fastcgi_params;
  15. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  16. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  17. fastcgi_index index.php;
  18. fastcgi_pass 127.0.0.1:9000;
  19. }
  20. }

HipHop Virtual Machine

Your HipHop Virtual Machine configuration file should contain this code (along with other settings you may need). Be sure you change the SourceRoot setting to point to your Slim app’s document root directory.

  1. Server {
  2. SourceRoot = /path/to/public/directory
  3. }
  4. ServerVariables {
  5. SCRIPT_NAME = /index.php
  6. }
  7. VirtualHost {
  8. * {
  9. Pattern = .*
  10. RewriteRules {
  11. * {
  12. pattern = ^(.*)$
  13. to = index.php/$1
  14. qsa = true
  15. }
  16. }
  17. }
  18. }

IIS

Ensure the Web.config and index.php files are in the same public-accessible directory. The Web.config file should contain this code:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <configuration>
  3. <system.webServer>
  4. <rewrite>
  5. <rules>
  6. <rule name="slim" patternSyntax="Wildcard">
  7. <match url="*" />
  8. <conditions>
  9. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  10. <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
  11. </conditions>
  12. <action type="Rewrite" url="index.php" />
  13. </rule>
  14. </rules>
  15. </rewrite>
  16. </system.webServer>
  17. </configuration>

lighttpd

Your lighttpd configuration file should contain this code (along with other settings you may need). This code requires lighttpd >= 1.4.24.

  1. url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")

This assumes that Slim’s index.php is in the root folder of your project (www root).