dpevsxjn809817 2011-03-31 13:17
浏览 12

我的Symfony项目的移动版本

I am creating the mobile version in a symfony project and I am using the technique described here: http://symfony.com/blog/how-to-create-an-optimized-version-of-your-website-for-the-iphone-in-symfony-1-1

So far it is working, but I have one problem: Most of my standard pages are perfectly valid to browse with a mobile phone but symfony forces me to create the *Success.mobile.php templates... I would like symfony to use the normal template if it does not find the .mobile.php one. Is that possible? How would you solve it?

  • 写回答

1条回答 默认 最新

  • dpmpa26468 2011-03-31 21:54
    关注

    You have to check before rendering if that template exists, and if it doesn't, set the default template. This can be done by adding a filter that check that. So...

    Add this filter to a lib/ folder, for example /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();
        }
    }
    

    Then add to your filters.yml

    <!-- /apps/frontend/config/filters.yml -->
    rendering: ~
    security:  ~
    
    # insert your own filters here
    ViewFilter:
     class: ViewFilter
    
    cache:     ~
    execution: ~
    

    And should be working :) If you do not know what is a filter and what it does please refer to Symfony's Filters Guide to get you started.

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目