开发者

Mobile version of my Symfony project

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 .mobil开发者_JAVA技巧e.php one. Is that possible? How would you solve it?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜