2011-03-31 44 views
0

我创建一个symfony项目的移动版本,我使用这里描述的技术的手机版:到目前为止,它正在http://symfony.com/blog/how-to-create-an-optimized-version-of-your-website-for-the-iphone-in-symfony-1-1我Symfony的项目

,但我有一个问题:我的大多数标准页用手机浏览是完全有效的,但symfony迫使我创建* Success.mobile.php模板...我希望symfony在找不到.mobile.php文件时使用普通模板。那可能吗?你会如何解决它?

回答

5

如果该模板存在,则必须在渲染前检查,如果不存在,请设置默认模板。这可以通过添加一个检查过滤器来完成。所以......

这个过滤器添加到一个lib /文件夹,例如/lib/filters/ViewFilter.class.php

<!-- /lib/filters/ViewFilter.class.php --> 
class ViewFilter extends sfFilter{ 
    public function execute($filterChain){ 
     if ($this->isFirstCall()){ 
      //get context 
      $context = $this->getContext(); 
      //get module name 
      $module = $context->getModuleName(); 
      //get action name 
      $action = $context->getActionName(); 

      //get template file name for this request 
      $templateFile = $action . "Success.mobile.php"; 
      //set physical path of that template 
      $path = sfConfig::get('sf_app_module_dir').DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR."templates".DIRECTORY_SEPARATOR. $templateFile; 
      //check if exists 
      if(!file_exists($path)) 
       //if is not, set html format to render the {$action}Success.php 
       $context->getRequest()->setRequestFormat('html'); 

     } 

     $filterChain->execute(); 
    } 
} 

然后添加到您的filters.yml

<!-- /apps/frontend/config/filters.yml --> 
rendering: ~ 
security: ~ 

# insert your own filters here 
ViewFilter: 
class: ViewFilter 

cache:  ~ 
execution: ~ 

并且应该正在工作:) 如果您不知道什么是过滤器以及它的功能,请参阅Symfony's Filters Guide以帮助您入门。

+0

我正在考虑向操作添加代码;这太好了! – Nathan 2011-03-31 23:57:16

+0

这看起来不错。我会稍后检查它是否有效:) – miguelSantirso 2011-04-01 07:06:51

+0

它适合你吗? – Pabloks 2011-06-23 18:46:16