Tuesday, March 29, 2011

Create the Mobile Version of a Symfony WebSite

Change the config/ProjectConfiguration.class.php file. Add a new row to the setup method and add a completely new method to the class:
public function setup()
  {
    ...

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

  public function filterRequestParameters(sfEvent $event, $parameters)
  {
    $request = $event->getSubject();

    if(preg_match('#^(?!.*iPad).*(Mobile|Jasmine|Symbian|NetFront|BlackBerry|Opera Mini|Opera Mobi).*$#i', $request->getHttpHeader('User-Agent')))
    {
      $request->setRequestFormat('mobile');
    }

    return $parameters;
  }
That's all. Now you have to create the mobile version of your layout (layout.mobile.php), your templates (ex. showSuccess.mobile.php), your partials (ex. _menu.mobile.php). Then you can try to open your site with your mobile, or you can use an emulator like Opera Mobile Emulator.

ps: you can use the $request->getRequestFormat() in an action which returns with "mobile".
ps2: Useful urls:
How to create an optimized version of your website for the iPhone in symfony 1.1
WebMozarts - Creating mobile symfony sites
Symfony users - mobile site strategy  

No comments:

Post a Comment