聪明的PHP模板,mod重写更改URL,但不更改页面


Smarty PHP templates, mod rewrite changing URL but not page

我现在已经重写了这个问题,因为我认为问题与URL重写无关。

我正在用PHP和Smarty构建一个网站,并使用apache的mod重写使用相当常见的url重写。

在本地,如果我访问:example.com/search/?q=测试,我得到"测试"的搜索结果/search不是一个真正的目录,它正在转发到search.php?q=

当我上传时,它不起作用。我已经启用了sudo a2enmod rewrite重写,并且url正在更新,浏览器显示example.com/search/?地址栏中的q=test,不会抛出404,而是提供我的index.php页面。

我已经意识到问题可能与我的Smarty缓存有关。我有一个URL变量,它触发了要重建的缓存,?rebuildCacheNow

如果我转到/account,我会得到索引页面,但如果我转到/ancount?rebuildCacheNow将显示帐户页面。

如果我回到索引/,它仍然显示帐户页面。去/?rebuildCacheNow显示主页。

这是我的主要Smarty模板:

{* Smarty *}
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"><![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en"><!--<![endif]-->
{include 'head.tpl'}
<body id="top">
{include 'header.tpl'}
<div class="content-container">
    <div class="container content">
      {include $page.content}
    </div>
</div>
{include 'footer.tpl'}
<script src="{$JQUERY_CDN}"></script>
<script src="{$BOOTSTRAP_JS_CDN}"></script>
<script src="/js/main.js"></script>
{$page.scripts}
</body>
</html>

每个页面都提供这个模板,但我使用PHP来分配$page.content。因此/buy页面设置$page.content = 'buy.tpl'/search设置$page.content = 'search.tpl'

但看起来main.tpl正在被缓存——有没有更好的方法可以使用1个主大纲模板并在其中嵌入内容模板?

好的,我已经解决了。因此,在本地,我将rebuildCacheNow设置为每页都关闭,以确保每次都能看到更改——这就是为什么我没有注意到它

当我计算出它与main.tpl的缓存有关时,检查Smarty文档表明我可以为渲染的模板分配cache_id:

http://www.smarty.net/docsv2/en/caching.multiple.caches.tpl

因此,当我呈现Smarty模板时,我会根据$page.content变量的散列为其提供缓存ID:

// make cache id, based on template path
$cache_id = sha1( $page[ 'content' ] );
$smarty->display( 'main.tpl', $cache_id );