config/packages/nelmio_cors.yaml).OPTIONS requests, which is critical for modern SPAs and APIs. Avoids manual CORS header injection in every route/controller.add_header, Apache .htaccess) for full coverage.CorsMiddleware), but the bundle provides batteries-included features (e.g., preflight, path matching).league/cors).OPTIONS requests and header injection (e.g., PHPUnit + symfony/browser-kit).league/cors or zendframework/zend-diactoros + custom middleware..htaccess).OPTIONS handling, inconsistent headers).composer require nelmio/cors-bundle
config/packages/nelmio_cors.yaml with global/path-specific rules.nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/api/':
origin_regex: true
allow_origin: ['https://trusted-client.example.com']
location ~* \.(js|css|png|jpg)$ {
add_header 'Access-Control-Allow-Origin' '%env(CORS_ALLOW_ORIGIN)%';
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
}
OPTIONS preflight responses.symfony/security-bundle) for auth+CORS interactions.api-platform/core) if using auto-generated routes.paths configuration for route-specific rules./admin to internal origins only.config/packages/nelmio_cors.yaml).%env(CORS_ALLOW_ORIGIN)%).OPTIONS) requests for debugging.OPTIONS handling: Ensure bundle is enabled and routes are covered.add_header).allow_origin regex/configuration.OPTIONS) may increase load for high-traffic APIs.max_age in CORS headers to reduce preflight requests from browsers.max_age: 86400 (24h cache for preflight responses).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Bundle misconfiguration | Blocked cross-origin requests | Use allow_origin: ['*'] in dev, restrict in prod. |
Missing OPTIONS handling |
Preflight failures | Ensure all routes are covered in paths config. |
| Static asset CORS misconfigured | JS/CSS blocked in browsers | Automate web server config validation. |
| Symfony upgrade breaks compatibility | Bundle fails to load | Test upgrades in staging; check Symfony docs. |
| Overly permissive CORS rules | Security vulnerabilities (e.g., CSRF) | Audit rules regularly; use allow_origin whitelists. |
How can I help you explore Laravel packages today?