add docs for CORS management
This commit is contained in:
+14
-3
@@ -7,12 +7,15 @@ This API now includes comprehensive CORS (Cross-Origin Resource Sharing) managem
|
|||||||
## How It Works
|
## How It Works
|
||||||
|
|
||||||
### Automatic CORS Header Application
|
### Automatic CORS Header Application
|
||||||
|
|
||||||
All API responses automatically include CORS headers when CORS is enabled. This allows web applications from configured origins to access your API.
|
All API responses automatically include CORS headers when CORS is enabled. This allows web applications from configured origins to access your API.
|
||||||
|
|
||||||
### Preflight Request Handling
|
### Preflight Request Handling
|
||||||
|
|
||||||
The API automatically handles preflight OPTIONS requests (sent by browsers before actual requests to CORS-protected resources). These are handled with a 204 No Content response.
|
The API automatically handles preflight OPTIONS requests (sent by browsers before actual requests to CORS-protected resources). These are handled with a 204 No Content response.
|
||||||
|
|
||||||
### Origin Validation
|
### Origin Validation
|
||||||
|
|
||||||
Only requests from configured allowed origins are accepted. Requests from unauthorized origins are rejected.
|
Only requests from configured allowed origins are accepted. Requests from unauthorized origins are rejected.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
@@ -37,7 +40,7 @@ Edit `config/config.php` to configure CORS:
|
|||||||
## Configuration Options
|
## Configuration Options
|
||||||
|
|
||||||
| Option | Type | Default | Description |
|
| Option | Type | Default | Description |
|
||||||
|--------|------|---------|-------------|
|
| ------------------- | ----- | ------------------------------------------------------- | ----------------------------------------------------------------------------------- |
|
||||||
| `enabled` | bool | `true` | Enable or disable CORS |
|
| `enabled` | bool | `true` | Enable or disable CORS |
|
||||||
| `allowed_origins` | array | `[]` | List of domains allowed to access the API. Use `'*'` to allow all (NOT recommended) |
|
| `allowed_origins` | array | `[]` | List of domains allowed to access the API. Use `'*'` to allow all (NOT recommended) |
|
||||||
| `allowed_methods` | array | `GET, POST, PUT, DELETE, PATCH, OPTIONS` | HTTP methods allowed |
|
| `allowed_methods` | array | `GET, POST, PUT, DELETE, PATCH, OPTIONS` | HTTP methods allowed |
|
||||||
@@ -49,6 +52,7 @@ Edit `config/config.php` to configure CORS:
|
|||||||
## Setup for Different Environments
|
## Setup for Different Environments
|
||||||
|
|
||||||
### Development
|
### Development
|
||||||
|
|
||||||
```php
|
```php
|
||||||
'cors' => [
|
'cors' => [
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
@@ -63,6 +67,7 @@ Edit `config/config.php` to configure CORS:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Production
|
### Production
|
||||||
|
|
||||||
```php
|
```php
|
||||||
'cors' => [
|
'cors' => [
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
@@ -76,6 +81,7 @@ Edit `config/config.php` to configure CORS:
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Allow All Origins (NOT RECOMMENDED for Production)
|
### Allow All Origins (NOT RECOMMENDED for Production)
|
||||||
|
|
||||||
```php
|
```php
|
||||||
'cors' => [
|
'cors' => [
|
||||||
'enabled' => true,
|
'enabled' => true,
|
||||||
@@ -88,7 +94,7 @@ Edit `config/config.php` to configure CORS:
|
|||||||
|
|
||||||
When making cross-origin requests with certain headers or methods (like PUT or DELETE), browsers automatically send a preflight OPTIONS request. The API handles these automatically:
|
When making cross-origin requests with certain headers or methods (like PUT or DELETE), browsers automatically send a preflight OPTIONS request. The API handles these automatically:
|
||||||
|
|
||||||
```
|
```text
|
||||||
Browser sends: OPTIONS /api/resource
|
Browser sends: OPTIONS /api/resource
|
||||||
API responds: 204 No Content + CORS headers
|
API responds: 204 No Content + CORS headers
|
||||||
Browser sees: Request is allowed, proceeds with actual request
|
Browser sees: Request is allowed, proceeds with actual request
|
||||||
@@ -97,6 +103,7 @@ Browser sees: Request is allowed, proceeds with actual request
|
|||||||
## Testing CORS
|
## Testing CORS
|
||||||
|
|
||||||
### Using curl
|
### Using curl
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Test CORS with curl
|
# Test CORS with curl
|
||||||
curl -H "Origin: http://localhost:3000" \
|
curl -H "Origin: http://localhost:3000" \
|
||||||
@@ -107,7 +114,8 @@ curl -H "Origin: http://localhost:3000" \
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Expected Response Headers
|
### Expected Response Headers
|
||||||
```
|
|
||||||
|
```text
|
||||||
HTTP/1.1 204 No Content
|
HTTP/1.1 204 No Content
|
||||||
Access-Control-Allow-Origin: http://localhost:3000
|
Access-Control-Allow-Origin: http://localhost:3000
|
||||||
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
|
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
|
||||||
@@ -149,16 +157,19 @@ if ($corsManager->isPreflightRequest()) {
|
|||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
### Preflight request fails
|
### Preflight request fails
|
||||||
|
|
||||||
- Check that the origin in the request matches one in `allowed_origins`
|
- Check that the origin in the request matches one in `allowed_origins`
|
||||||
- Verify CORS is enabled in config
|
- Verify CORS is enabled in config
|
||||||
- Check browser console for CORS error messages
|
- Check browser console for CORS error messages
|
||||||
|
|
||||||
### Missing CORS headers in response
|
### Missing CORS headers in response
|
||||||
|
|
||||||
- Ensure CORS is enabled: `'enabled' => true`
|
- Ensure CORS is enabled: `'enabled' => true`
|
||||||
- Verify the requesting origin is in `allowed_origins`
|
- Verify the requesting origin is in `allowed_origins`
|
||||||
- Check that `CorsManager` is properly initialized in the container
|
- Check that `CorsManager` is properly initialized in the container
|
||||||
|
|
||||||
### "No Access-Control-Allow-Origin header"
|
### "No Access-Control-Allow-Origin header"
|
||||||
|
|
||||||
- Browser origin is not in `allowed_origins`
|
- Browser origin is not in `allowed_origins`
|
||||||
- Add the origin or use `'*'` (only for development)
|
- Add the origin or use `'*'` (only for development)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user