Mobile version of my Symfony project

£可爱£侵袭症+ 提交于 2019-12-11 23:40:00

问题


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:


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.



来源:https://stackoverflow.com/questions/5500488/mobile-version-of-my-symfony-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!