dscojuxf69080 2013-05-05 17:31
浏览 58
已采纳

保持动态导航栏内容与控制器代码分开 - Symfony2

I'm new to Symfony2 and I've been thinking of the best way to generate navigation bar HTML that is used on every page request - especially with regards to caching.

So imagine if every page request shows the logged in user and a number indicating how many messages are unread (in fact like stackoverflow for that). I'm guessing that could be generated in every controller to ensure the info is up-to-date (using a function or something of course) - but I'm also looking at caching the whole controller output, and think it might be good to keep this dynamic part separate.

Would creating a controller extension for all this kind of stuff be a good way to go? So this way the controller only deals with that specific function (e.g. getting blog posts from a DB etc.) and the controller extension adds all the dynamic content. That way I can cache the controller result and speed up pages without caching the full page (which can't really be done due to lots of dynamic HTML content).

Something like this maybe:

class ControllerExtension extends Controller
{
    public function render($view, array $parameters = array(), Response $response = null)
    {

        //get number of messages for this user
        $parameters['messages'] =

        //are they logged in
        $parameters['logged_in'] =

        // render as normal
        return parent::render($view, $parameters, $response);

    }
}

For this I want to ignore use of JS. I know some of these things could be populated with JS, but I would prefer not for this.

  • 写回答

1条回答 默认 最新

  • dqy92287 2013-05-05 18:33
    关注

    You can solve this by caching the navbar fragment separably from the page html with ESI or Hinclude and can be simply and elegantly solved with Symfony2.

    Embed a controller inside a template

    You can render a controller inside a template:

    <div id="sidebar">
        {% render url('latest_articles', { 'max': 3 }) %}
    </div>
    

    This will render the controller with the route "latest_articles", inside your html.

    This can be done in you controller template, or in the global layout template (where you define the header, footer, js, css ecc of all your pages, see Template Inheritance)

    Cache the embedded fragment separately from the page html:

    You can use a reverse proxy (like Varnish, or the AppCache), to cache the two part of the html separately:

    <div id="sidebar">
        {% render url('latest_articles', { 'max': 3 }, {'standalone': true}) %}
    </div>
    

    That's it, just add {'standalone': true}

    You'll need an external program on front of your web server (like Varnish, or Nginx with a mod), but this is the fastest way.

    Load the fragment with javascript:

    You can also tell symfony to asynchronously load the fragment in javascript:

    <div id="sidebar">
        {% render url('latest_articles', { 'max': 3 }, {'standalone': 'js'}) %}
    </div>
    

    This is a nice approach, since you can cache the entire html in a CDN (for example with Amazon CDN CloudFront), and still show user specific content.

    For info see: http://symfony.com/doc/2.1/book/templating.html#asynchronous-content-with-hinclude-js

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测