php - 如何在 Symfony 1.4 中实现 Mobile 版本?

标签 php mobile symfony1 symfony-1.4

实现 Symfony 1.4 中开发的 Web 应用程序的移动版本的最佳方法是什么?我在谷歌上搜索但没有成功。

真的不知道如何开始或是否有任何标准可遵循。我应该实现不同的模板还是完全不同的应用程序(例如前端/后端)。

我的第一个问题是实现一个过滤器,它的作用类似于 onRequest 监听器,并且如果用户代理是移动的,则重定向到移动应用程序。但我认为通过这种方式(使用不同的应用程序)很难从移动版本迁移到浏览器版本,反之亦然。

最佳答案

这几乎与 symfony 网站上定义的方式完全相同。

查看文章:How to create an optimized version of your website for the iPhone in symfony 1.1 ,特别是这部分:

// config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    // ...

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

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

    if (preg_match('#Mobile/.+Safari#i', $request->getHttpHeader('User-Agent')))
    {
      $request->setRequestFormat('iphone');
    }

    return $parameters;
  }
}

现在,来自 iPhone 的每个请求都将使用 *Success.iphone.php 模板,而不是 *Success.php 模板。

正如你所说,从一个环境切换到另一个环境会更困难。就像从 iPhone 上查看完整的网站一样。为此,您应该使用带有诸如 ?doaction=read_on_site 之类的参数的 session 来处理它,然后设置一个 cookie 并在 filterRequestParameters 内进行检查。

编辑:

使用过滤器会更容易。轻松访问请求和响应

<?php 

class iPhoneFilter extends sfFilter
{
  public function execute ($filterChain)
  {
    // get the cool stuff
    $context    = $this->getContext();
    $request    = $context->getRequest();
    $response   = $context->getResponse();

    if ($this->isFirstCall())
    {
      $cookies = $response->getCookies();
      if (preg_match('#Mobile/.+Safari#i', $request->getHttpHeader('User-Agent'))
       && ! isset($cookies['doaction']))
      {
        $request->setRequestFormat('iphone');
      }
    }

    // execute next filter
    $filterChain->execute();
  }
}

关于php - 如何在 Symfony 1.4 中实现 Mobile 版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12349375/

相关文章:

jquery - 如何让 jQuery 移动 ListView 与复选框和图标一起工作

api - 使用 Symfony 实现长轮询 API

php - 查询从 symfony 上的两个表中选择

jquery - Symfony ajax 字段验证

php - 按数组值排序

php - SimpleXML:获取属性可变的值

mobile - 移动 Web 浏览器中的 SSL 客户端证书身份验证

php - Laravel Collection 日期比较

php - Behat 3 - 如何在上下文中检索自定义扩展

css - Blackberry Curve 9300 和 Bold 9650 - 在 native 浏览器中支持 CSS 背景图像吗?