如何使用 Silex 为每个响应“自动”添加标头?
到目前为止,我必须对每个回复进行以下操作:
$app->post('/photos'), function () use ($app) {
return $app->json(array('status' => 'success'), 200, array('Access-Control-Allow-Origin' => '*'));
});
相反,我想使用 before 过滤器Access-Control-Allow-Origin: *
自动发送每个请求:
// Before
$app->before(function () use ($app) {
$response = new Response();
$response->headers->set('Access-Control-Allow-Origin', '*');
});
// Route
$app->post('/photos'), function () use ($app) {
return $app->json(array('status' => 'success')); // <-- Not working, because headers aren't added yet.
});