Thursday, October 7, 2010

Symfony 1.4 Automatically detect iPhone and use different pages

Note
This will make the whole site iphone aware, which means every single page must have an iphone page.

Changes in config/factories.yml
This will make Symfony know what to send when it is an iPhone format.

all:
  ...
  request:
    class: sfWebRequest
    param:
      formats:
        iphone: text/html

Changes in config/ProjectConfiguration.class.php
This will make Symfony look for the iPhone user-agent and change the format accordingly

public function setup()
  {
    ...
  
    $this->dispatcher->connect('request.filter_parameters', array($this, 'filterRequestParameters'));
  }

  public function filterRequestParameters(sfEvent $event, $parameters) {
    $request = $event->getSubject();
  
    $x = strpos($request->getHttpHeader('User-Agent'), 'Mobile');
    if ($x != '') {
      $x = '';
      $x = strpos($request->getHttpHeader('User-Agent'), 'Safari');
      if ($x != '') {
        $request->setRequestFormat('iphone');
      }
    }
    return $parameters;
  }

No comments:

Post a Comment