symfony Разрешить HTTP OPTIONS для всех запросов - всех методов контроллера - без бандла
Primary tabs
Можно сделать через костыль, создать класс:
<?php class OptionsHelper { public static $test = 'uknown'; public static function sendOptionsResponse() { $method = $_SERVER['REQUEST_METHOD']; if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { $test22= 'options'; $origin = !empty($_SERVER['HTTP_ORIGIN']) && self::isOriginAllowed($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : ''; header('Access-Control-Allow-Origin: ' . $origin); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); header('Access-Control-Max-Age: 1'); header('Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description, X-AUTH-TOKEN, Set-Cookie'); die(); } } protected static function isOriginAllowed(string $origin): bool { return ($origin === 'вашДоменСПротоколомИПортом); } }
И использовать его в public/index.php:
<?php use App\Kernel; // -------- @todo Добавить более изящное решение для разрешения OPTIONS require_once 'OptionsHelper.php'; OptionsHelper::sendOptionsResponse(); //------------------- require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; return function (array $context) { return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); };
Альтернативное решение
- Log in to post comments
- 41 reads