2011-03-31 65 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