Wednesday, December 21, 2011

Using Symfony2 with PHP 5.4 built-in web server

PHP 5.4 comes with built-in web server. If you want to use it with Symfony2, you have to implement default mod_rewrite rules using a PHP router script:

<?php

if (isset($_SERVER['SCRIPT_FILENAME'])) {
    return false;
} else {
    $_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT']
        . DIRECTORY_SEPARATOR
        . 'app.php'
    ;

    require 'app.php';
}
Put this script into your project's web directory and run server (from that directory):
php -S ADDRESS:PORT router.php
You can also specify document root using "-t" option:
php -S ADDRESS:PORT -t DOCROOT/ DOCROOT/router.php

Built-in web server can be very useful if you want to quickly setup development environment or test latest PHP release. But remember that it was not designed for production use!

UPDATE (2012-03-15): Updated router script.