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.

Wednesday, October 19, 2011

Sending logs by email in Symfony2

If you want to send your logs by email in Symfony2, you have to set SwiftMailerHandler in monolog configuration:

monolog:
    handlers:
        [...]
        mail:
            type: swift_mailer
            from_email: from_address@example.com
            to_email: to_address@example.com
            subject: Error messages
            level: ERROR

Above configuration will send only error logs (one message per error). There's also NativeMailHandler which uses native PHP's mail() function.