Rewriting

I strongly encourage you to use a web server that supports URL rewriting; this will let you enjoy clean, human-friendlyURLs with your Slim application. To enable URL rewriting, you should use the appropriate tools provided by yourweb server to forward all HTTP requests to the PHP file in which you instantiate and run your Slim application.The following are sample, bare minimum, configurations for Apache with mod_php and nginx. These are not meant tobe production ready configurations but should be enough to get you up and running. To read more on server deploymentin general you can continue reading http://www.phptherightway.com.

Apache and mod_rewrite

Here is an example directory structure:

  1. /path/www.mysite.com/
  2. public_html/ <-- Document root!
  3. .htaccess
  4. index.php <-- I instantiate Slim here!
  5. lib/
  6. Slim/ <-- I store Slim lib files here!

The .htaccess file in the directory structure above contains:

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

You also need a directory directive to enable .htaccess files and allow the RewriteEngine directive to be used.This is sometimes done globally in the httpd.conf file, but its generally a good idea to limit the directive tojust your virtual host by enclosing it in your VirtualHost configuration block. This is generally setup in yourconfiguration in the form of:

  1. <VirtualHost *:80>
  2. ServerAdmin me@mysite.com
  3. DocumentRoot "/path/www.mysite.com/public_html"
  4. ServerName mysite.com
  5. ServerAlias www.mysite.com
  6. #ErrorLog "logs/mysite.com-error.log"
  7. #CustomLog "logs/mysite.com-access.log" combined
  8. <Directory "/path/www.mysite.com/public_html">
  9. AllowOverride All
  10. Order allow,deny
  11. Allow from all
  12. </Directory>
  13. </VirtualHost>

As a result, Apache will send all requests for non-existent files to my index.php script in which I instantiateand run my Slim application. With URL rewriting enabled and assuming the following Slim application is defined inindex.php, you can access the application route below at “/foo” rather than “/index.php/foo”.

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/foo', function () {
  4. echo "Foo!";
  5. });
  6. $app->run();

nginx

We will use the same example directory structure as before, but with nginx our configuration will go into nginx.conf.

  1. /path/www.mysite.com/
  2. public_html/ <-- Document root!
  3. index.php <-- I instantiate Slim here!
  4. lib/
  5. Slim/ <-- I store Slim lib files here!

Here is a snippet of a nginx.conf in which we use the try_files directive to serve the file if it exists,good for static files (images, css, js etc), and otherwise forward it on to the index.php file.

  1. server {
  2. listen 80;
  3. server_name www.mysite.com mysite.com;
  4. root /path/www.mysite.com/public_html;
  5. try_files $uri $uri/ /index.php?$query_string;
  6. # this will only pass index.php to the fastcgi process which is generally safer but
  7. # assumes the whole site is run via Slim.
  8. location /index.php {
  9. fastcgi_connect_timeout 3s; # default of 60s is just too long
  10. fastcgi_read_timeout 10s; # default of 60s is just too long
  11. include fastcgi_params;
  12. fastcgi_pass 127.0.0.1:9000; # assumes you are running php-fpm locally on port 9000
  13. }
  14. }

Most installations will have a default fastcgi_params file setup that you can just include as shown above.Some configurations don’t include the SCRIPT_FILENAME parameter. You must ensure you include this parameterotherwise you might end up with a No input file specified error from the fastcgi process. This can be done directlyin the location block or simply added to the fastcgi_params file. Either way it looks like this:

  1. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

Without URL Rewriting

Slim will work without URL rewriting. In this scenario, you must include the name of the PHP file in which youinstantiate and run the Slim application in the resource URI. For example, assume the following Slim applicationis defined in index.php at the top level of your virtual host’s document root:

  1. <?php
  2. $app = new \Slim\Slim();
  3. $app->get('/foo', function () {
  4. echo "Foo!";
  5. });
  6. $app->run();

You can access the defined route at “/index.php/foo”. If the same application is instead defined in index.phpinside of the physical subdirectory blog/, you can access the defined route at /blog/index.php/foo.