symfony - $app->finish not triggering -
got strange 1 here. i'm done silex app, having problems getting $app->finish trigger. here's code:
<?php require_once __dir__ . '/../vendor/autoload.php'; $app = new silex\application(); $app->get('/', function (request $request) { $batchprocess = function () { long_process(); }; $app->finish($batchprocess); return $app->json("ok", 200); }; $app->run();
so here's problem: batch process never runs! trying find bug, added var_export "on" function in silex\application:
/** * adds event listener listens on specified events. * * @param string $eventname event listen on * @param callable $callback listener * @param integer $priority higher value, earlier event * listener triggered in chain (defaults 0) */ public function on($eventname, $callback, $priority = 0) { $this['dispatcher'] = $this->share($this->extend('dispatcher', function ($dispatcher, $app) use ($callback, $priority, $eventname) { $dispatcher->addlistener($eventname, $callback, $priority); return $dispatcher; })); var_export($this['dispatcher']); }
when var_export in there, works (though kernel runs batch process before sending data). when var_export commented out, "ok" returned , batch process never runs.
what doing wrong? why kernel terminating without executing process?
there may way achieve 2 changes response class.
- the first adding
ob_flush()
beforeflush()
inresponse::send()
.- the second setting
content-length
header.
i able achieve same effect without modifying response
class. first, need move callback set in finish
middleware outside of get
method, , check manually route ('get_'
in case responds '/'
path):
$app->finish(function() { if ('get_' === $request->attributes->get('_route')) { long_process(); } });
then here how get
method should like:
$app->get('/', function(request $request) use ($app) { $content = json_encode(array( 'status' => 'ok', )); $response = new response($content, 200); $response->headers->set('content-type', 'application/json'); $response->headers->set('content-length', strlen($content)); $response->send(); ob_flush(); return $response; });
setting content-length important, or json content sent twice, $response returned in method, , sent silex.
Comments
Post a Comment