Sunday, September 5, 2010

Removing the .php extension from Symfony controller

If you wish to shorten your Symfony 1.4 URLs (http://www.yourdomain.com/symfonyApp.php/user/login) by removing the php extension from the controller (http://www.yourdomain.com/symfonyApp/user/login), then read on.

1. Update your factories.yml file and add the relative_url_root option as described below:

all:
  request:
    class: sfWebRequest
    param:
      logging:           %SF_LOGGING_ENABLED%
      path_info_array:   SERVER
      path_info_key:     PATH_INFO
      relative_url_root: ""
      formats:
        txt:  text/plain
        js:   [application/javascript, application/x-javascript, text/javascript]
        css:  text/css
        json: [application/json, application/x-json]
        xml:  [text/xml, application/xml, application/x-xml]
        rdf:  application/rdf+xml
        atom: application/atom+xml

2. Create a symbolic link in your web directory to your controller:

cd /your/symfony/project/dir/web
ln -s symfonyApp.php symfonyApp

3. Update your apache configuration to set your new default type:

DefaultType application/x-httpd-php

Warning: This means all your text files would be passed through the PHP interpreter! There would be a performance hit depending on the number of static files that your server hosts.

4. Reload Apache configuration

service http reload

You should now be able to make successful requests to your syfmony application by using the shorter URL version.