社交昵称/scn-esi-widget

使 ZF2 应用能够输出用于组件化和高度缓存应用的 ESI 标签。

1.1.1 2013-07-29 17:33 UTC

This package is auto-updated.

Last update: 2024-09-08 06:42:17 UTC


README

使 ZF2 应用能够输出用于组件化和高度缓存应用的 ESI 标签。

Build Status

需求

功能

  • 通过 EsiWidget 控制器插件将 ESI 组件添加到操作中
  • EsiStrategy 检测代理能力以选择 EsiRenderer
  • EsiRenderer 将子视图模型渲染为 ESI 标签
  • 如果没有代理能力则回退到 PHPRenderer(即开发环境中工作)

安装

建议使用 Composer 将此模块添加到您的 Zend Framework 2 应用程序中。在克隆 ZendSkeletonApplication 之后,将 "socalnick/scn-esi-widget" 添加到需求列表中,然后运行 php composer.phar install/update。您的 composer.json 应该看起来像这样

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.*",
        "socalnick/scn-esi-widget": "1.*"
    }
}

接下来,将所需模块添加到 config/application.config.php

<?php
return array(
    'modules' => array(
        'Application',
        'ScnEsiWidget',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

Varnish

安装

Varnish 可以安装在任何现代 Linux 发行版上:[https://www.varnish-cache.org/docs/3.0/installation/](https://www.varnish-cache.org/docs/3.0/installation/) 它也可以通过 Mac OSX 上的 [Homebrew](http://mxcl.github.com/homebrew/) 进行开发安装,只需运行 brew install varnish

配置

这是开发环境中最基础的 Varnish 配置。它设置了后端主机/端口,设置了一个请求头以指示代理能力,并查找响应 Surrogate Control 头以启动 ESI 处理。在生产环境中运行 Varnish 之前,强烈建议您在 [https://www.varnish-cache.org/docs](https://www.varnish-cache.org/docs) 上了解更多信息

backend default {
    .host = "127.0.0.1";
    .port = "10088";
}

sub vcl_recv {
    # Set a header announcing Surrogate Capability to the origin
    # ScnEsiWidget sees this header and emits ESI tag for widgets
    set req.http.Surrogate-Capability = "varnish=ESI/1.0";
}

sub vcl_fetch {
    # Unset the Surrogate Control header and do ESI
    if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
        unset beresp.http.Surrogate-Control;
        set beresp.do_esi = true;
    }
}

使用方法

调用 ESI 组件控制器插件

public function esiAction()
{
    $viewModel = new ViewModel();
    $this->esiWidget()->addToViewModel($viewModel, '/application/index/recent-tweets', 'recentTweets');

    $headers = $this->getResponse()->getHeaders();
    $cacheControl = new \Zend\Http\Header\CacheControl();
    $cacheControl->addDirective('s-maxage', '60');
    $headers->addHeader($cacheControl);

    return $viewModel;
}

在视图脚本中回显 ESI 组件

<div><?php echo $this->recentTweets ?></div>

创建 ESI 组件操作

public function recentTweetsAction()
{
    $headers = $this->getResponse()->getHeaders();
    $cacheControl = new \Zend\Http\Header\CacheControl();
    $cacheControl->addDirective('s-maxage', '10');
    $headers->addHeader($cacheControl);

    $viewModel = new ViewModel();
    $viewModel->setTerminal(true);

    return $viewModel;
}

为 ESI 组件操作创建视图脚本

<ul>
    <li><?php echo date('h:i:s')?> @SocalNick: This is a recent tweet!</li>
    <li><?php echo date('h:i:s', time() - 10)?> @SocalNick: This is a slightly less recent tweet!</li>
</ul>