composer require coka/cors-bundle
config/bundles.php:
CedrickOka\CorsBundle\CorsBundle::class => ['all' => true],
config/packages/coka_cors.yaml:
cors:
paths:
'^/api/*':
allow_origin: ['*']
allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: []
max_age: 3600
allow_credentials: false
/api/test. Verify headers in browser DevTools (Access-Control-Allow-Origin).Path-Based Configuration:
/api/* vs /admin/*)./admin to specific origins:
paths:
'^/admin/*':
allow_origin: ['https://trusted-domain.com']
Dynamic Origin Handling:
ALLOWED_ORIGINS in .env):
allow_origin: '%env(ALLOWED_ORIGINS)%'
allow_origin: ['%env(ORIGIN_1)%', '%env(ORIGIN_2)%']
Middleware Integration:
config/packages/coka_cors.yaml:
middleware: ['cors']
Custom Headers:
expose_headers for non-standard responses (e.g., X-RateLimit-Limit):
expose_headers: ['X-RateLimit-Limit', 'X-RateLimit-Remaining']
Credentials Support:
allow_credentials: true
allow_origin to be explicit (not '*').when clause in config to apply rules based on environment:
paths:
'^/api/*':
allow_origin: ['*']
when: '%kernel.environment% in [dev, staging]'
kernel.response to modify CORS headers dynamically:
// src/EventListener/CorsListener.php
public function onKernelResponse(GetResponseForControllerResultEvent $event) {
$response = $event->getResponse();
$response->headers->set('Access-Control-Allow-Origin', 'https://dynamic-origin.com');
}
Wildcard ('*') + Credentials Conflict:
Access-Control-Allow-Origin cannot be '*' if allow_credentials: true.['https://app.example.com']).Preflight Requests (OPTIONS):
OPTIONS method in allow_methods causes CORS failures for POST/PUT with custom headers.OPTIONS and ensure allow_headers matches frontend requests.Cache Headers:
max_age in seconds (e.g., 3600 = 1 hour). Set to 0 for dynamic origins.Access-Control-Max-Age in response headers.Symfony 5.3+ Compatibility:
symfony/http-kernel ^5.3.framework.middleware in config/packages/coka_cors.yaml:
framework:
middleware:
cors: CedrickOka\CorsBundle\Middleware\CorsMiddleware
Debugging:
Network tab) to inspect:
Access-Control-Allow-Origin (should match request origin).Access-Control-Allow-Methods (verify methods are listed).Custom Middleware:
CedrickOka\CorsBundle\Middleware\CorsMiddleware to add logic (e.g., IP-based origin whitelisting).use CedrickOka\CorsBundle\Middleware\CorsMiddleware as BaseCorsMiddleware;
class CustomCorsMiddleware extends BaseCorsMiddleware {
protected function getAllowedOrigins(): array {
if ($this->isTrustedIP($this->request->getClientIp())) {
return ['https://trusted.com'];
}
return ['*'];
}
}
Event Dispatching:
cors.pre_flight or cors.response events (if supported) to modify behavior dynamically.Configuration Overrides:
config/packages/override/coka_cors.yaml to merge settings without editing the main config.expose_headers to reduce payload size.max_age to a high value (e.g., 86400 for 24h) for static APIs.when: '%kernel.environment% != prod'
How can I help you explore Laravel packages today?