The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Tracking the script execution time in slim.

ilkr's Avatar

ilkr

06 Sep, 2015 06:01 PM

Hi,

I am try to print script execution time. But "slim.after.dispatch" hook seems not work.

Thanks.

index.php
<?php
$starttime = microtime(true);
.
.
.
$app->hook('slim.after.dispatch', function () use ($app, $starttime) {
    $app->view()->appendData(array(
        'execution_time' => microtime(true) - $starttime
    ));
});
// Run app
$app->run();

footer.twig
<div id="footer">
{{ execution_time }}
</div>

  1. 1 Posted by Monique Alexand... on 22 Dec, 2015 09:42 PM

    Monique Alexander's Avatar

    Slim renders template before calling 'slim.after.dispatch'. So {{ execution_time }} in your template is always set to null.

    You can pass execution time in headers/cookies, or just echo after closing html tag, and then display in in correct place using javascript.

    Here's the code I use:

    $starttime = microtime(true);

    $app->hook('slim.after', function () use ($app, $starttime) {
        echo "<!-- " . (microtime(true) - $starttime) . " -->";
    });

  2. 2 Posted by marcin on 23 Dec, 2015 12:08 PM

    marcin's Avatar

    Here's the code I use in slim 3:

    $starttime = microtime(true);

    $app->add(function (Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, $next) use ($starttime) {
        $response = $next($request, $response);
        $response->getBody()->write("<!-- " . (microtime(true) - $starttime) . " -->");
        return $response;
    });

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac