我的Symfony项目的移动版本 [英] Mobile version of my Symfony project
问题描述
我正在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
到目前为止,它仍在工作,但我有一个问题:我的大多数标准页面都可以用手机浏览,完全有效,但是symfony迫使我创建* Success.mobile.php模板...我想要symfony如果找不到.mobile.php,则使用普通模板.那可能吗?您将如何解决?
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?
推荐答案
您必须在渲染之前检查该模板是否存在,如果不存在,请设置默认模板.这可以通过添加检查该过滤器的过滤器来完成.所以...
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...
将此过滤器添加到lib/文件夹,例如/lib/filters/ViewFilter.class.php
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();
}
}
然后将其添加到您的filter.yml
Then add to your filters.yml
<!-- /apps/frontend/config/filters.yml -->
rendering: ~
security: ~
# insert your own filters here
ViewFilter:
class: ViewFilter
cache: ~
execution: ~
并且应该正常工作:) 如果您不知道什么是过滤器及其作用,请参阅 Symfony的过滤器指南让您入门.
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.
这篇关于我的Symfony项目的移动版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!