-->

Silex “->after” middleware usage

2019-07-25 13:53发布

问题:

I am testing Silex "->after" middleware to set the response header to enable CORS access. Silex is running V2.0.4.

My index.php excerpt as below:

$app->get('/{entity}/{func}', function ($entity, $func) use ($app) {
    if (method_exists($namespace, $func))
    {
        $result = call_user_func_array([$namespace, $func], [$app]);
        $status = 200;
    }
    $out = ['status' => $status, 'out' => $result];
    return json_encode($out);
})->after(function (Request $request, Response $response) {
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->headers->set('Access-Control-Allow-Headers', 'Authorization');
    });

But the api will not display anything. If taken out the ->after portion, it returns the correct JSON string retrieved by relevant methods.

Would appreciate any hints on this.

标签: silex