如果该模板存在,则必须在渲染前检查,如果不存在,请设置默认模板。这可以通过添加一个检查过滤器来完成。所以......
这个过滤器添加到一个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以帮助您入门。
我正在考虑向操作添加代码;这太好了! – Nathan 2011-03-31 23:57:16
这看起来不错。我会稍后检查它是否有效:) – miguelSantirso 2011-04-01 07:06:51
它适合你吗? – Pabloks 2011-06-23 18:46:16