We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Micro Application(facing prob with finish middleware event)

I'm facing a problem while implementing micro application in which I want a middle ware that is executed after response is send to browser. As mentioned in documentation of Phalcon finish middle ware suppose to that work but its not working, browser still waiting to complete whole process then it return response.

my code is like:

$app->before(function() use ($app, $di) {
        $di->get('log')->log("This is a message before");
        $di->get('log')->close();
    });

    $testCtrl = new testCtrl();
    $app->get('/ctrltest', array($testCtrl, "indexAction"));

    $app->after(function() use ($app, $di) {
         echo "after";
        $di->get('log')->log("This is a message after");
        $di->get('log')->close();
    });

    $app->finish(function() use ($app, $di) {
            $count = 100000;
            while ($count) {
                $di->get('log')->log("count " . $count);
                $count--;
            }
    });

Response are coming after whole loop is executed. Any sample code or some suggestion will be helpful

You are looking for fastcgi_finish_request

https://php.net/manual/en/function.fastcgi-finish-request.php

$app->after(function() use ($app, $di) {
    fastcgi_finish_request();
    $di->get('log')->log("This is a message after");
    $di->get('log')->close();
});


853

Thanks david, But this function fastcgi_finish_request(); thowing a error Call to undefined function fastcgi_finish_request()

edited Nov '15

You need to use FPM as SAPI in order to use php fastcgi_finish_request();

https://php.net/manual/en/ref.fpm.php