Zend 2 中请求之间的时间 [英] Time between request in Zend 2

查看:26
本文介绍了Zend 2 中请求之间的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Zend Framework 2 的问题.我想知道完成一个请求需要多长时间,我知道使用 Zend MVCEvent 应该是可能的,但我不知道如何.

解决方案

我认为更好的解决方案是像这样替换模板文件中的特殊变量

{{ renderTime }}

因此您可以自由控制文本位置,并且不会在 zf 应用程序中弄乱 json 响应模型.

让我解释一下为什么我不连接到 \Zend\View\ViewEvent::EVENT_RENDERER 事件并直接在 Response 中替换我的变量 Responsecode>FINISH 事件.

基本上你不能使用其他事件,因为页面不会在任何时候呈现,并且模板中的所有函数/视图助手当前都没有完成,所以时间会少于真正的呈现时间.

\Zend\View\ViewEvent::EVENT_RENDERER_POST 事件不好,因为 - 你的视图被渲染为正确 - 但应用程序没有完成,而且可能所有随后的注册事件等都没有尚未执行.

如果你喜欢我的想法,那么你的 index.php 应该是这样的

run();

和你的 module.php onBootstrap 方法会变成这个

$e->getApplication()->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($e){/** @var $e \Zend\Mvc\MvcEvent */if( $e->getResult() instanceof \Zend\View\Model\ViewModel ){$renderText = sprintf('页面渲染时间 - %s 秒', number_format(microtime(true) - STARTTIME, 5));/** @var \Zend\Http\PhpEnvironment\Response $response */$response = $e->getResponse();$response->setContent(str_replace('{{ renderTime }}', $renderText, $response->getContent()));}}, 100000);

并在您希望使用 {{ renderTime }}

注入渲染时间的模板/布局文件集中

Hello Friends!

<div>一些内容</div><div class="footer">{{ 渲染时间 }}

现在为您提供页面渲染时间 - 0.67307 秒

I have a question about Zend Framework 2.I want to find out how much time take to complete a request and I know that it should be possible using Zend MVCEvent but I don't know how.

解决方案

a better solution in my mind is to replace a special variable inside your template files like this

{{ renderTime }}

so you are free to controll the text position and don't mess up json response models in your zf application.

let me explain why i don't hook up into the \Zend\View\ViewEvent::EVENT_RENDERER event and replace my variable directly in the Response at the FINISH event.

basically you can't use the other events, because the page is not rendered at any time and all the functions/viewhelper into your template are currently not done and so the time would be less then the real render time.

The \Zend\View\ViewEvent::EVENT_RENDERER_POST Event is not good because - your view is rendered thats true - but the application is not done and maybee all the registered events etc that follow are not executed yet.

if you like my idea than your index.php would look like this

<?php
/**
 * This makes our life easier when dealing with paths. Everything is relative
 * to the application root now.
 */
chdir(dirname(__DIR__));
define('STARTTIME', microtime(true));

// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server' && is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
    return false;
}

// Setup autoloading
require 'init_autoloader.php';

// Run the application!
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

and your module.php onBootstrap method would become this

$e->getApplication()->getEventManager()->attach(\Zend\Mvc\MvcEvent::EVENT_FINISH, function($e){
    /** @var $e \Zend\Mvc\MvcEvent */
    if( $e->getResult() instanceof \Zend\View\Model\ViewModel )
    {
        $renderText = sprintf('page render time - %s seconds', number_format(microtime(true) - STARTTIME, 5));

        /** @var \Zend\Http\PhpEnvironment\Response $response */
        $response = $e->getResponse();
        $response->setContent(
            str_replace('{{ renderTime }}', $renderText, $response->getContent())
        );
    }
}, 100000);

and in your template/layout file set where you wish to inject the rendertime with {{ renderTime }}

<h1>Hello Friends!</h1>
<div>some content</div>
<div class="footer">
    {{ renderTime }}
</div>

that now gives you page render time - 0.67307 seconds

这篇关于Zend 2 中请求之间的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆