You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lightweight MVC (Model-View-Controller) framework component for PHP 8.4+ that provides core MVC functionality including controllers, views, routing integration, request handling, and a powerful view caching system.
@@ -529,12 +529,65 @@ class NotFoundController extends HttpCodes
529
529
530
530
### View Caching
531
531
532
-
The framework includes a sophisticated view caching system:
532
+
The framework includes a sophisticated view caching system with multiple storage backends:
533
+
534
+
#### Storage Backends
535
+
536
+
1. **File Storage** (Default): Uses the local filesystem for cache storage
537
+
2. **Redis Storage**: High-performance in-memory caching with Redis
538
+
539
+
#### Features
533
540
534
541
1. **Automatic Cache Key Generation**: Based on controller, view, and data
535
542
2. **Selective Caching**: Enable/disable per view type
536
543
3. **TTL Support**: Configure expiration times
537
544
4. **Garbage Collection**: Automatic cleanup of expired entries
545
+
5. **Multiple Storage Backends**: Choose between file or Redis storage
546
+
547
+
#### Configuration
548
+
549
+
##### File Storage Configuration
550
+
```yaml
551
+
cache:
552
+
enabled: true
553
+
storage: file
554
+
path: cache/views
555
+
ttl: 3600
556
+
views:
557
+
html: true
558
+
markdown: true
559
+
json: false
560
+
xml: false
561
+
```
562
+
563
+
##### Redis Storage Configuration
564
+
```yaml
565
+
cache:
566
+
enabled: true
567
+
storage: redis # Use Redis instead of file storage
568
+
ttl: 3600
569
+
# Redis configuration (flat structure for env variable compatibility)
570
+
redis_host: 127.0.0.1
571
+
redis_port: 6379
572
+
redis_database: 0
573
+
redis_prefix: neuron_cache_
574
+
redis_timeout: 2.0
575
+
redis_auth: null # Optional: Redis password
576
+
redis_persistent: false # Optional: Use persistent connections
577
+
# View-specific cache settings
578
+
html: true
579
+
markdown: true
580
+
json: false
581
+
xml: false
582
+
```
583
+
584
+
This flat structure ensures compatibility with environment variables:
585
+
- `CACHE_STORAGE=redis`
586
+
- `CACHE_REDIS_HOST=127.0.0.1`
587
+
- `CACHE_REDIS_PORT=6379`
588
+
- etc.
589
+
590
+
#### Programmatic Usage
538
591
539
592
```php
540
593
// Cache is automatically used when enabled
@@ -546,17 +599,40 @@ $html = $this->renderHtml(
546
599
);
547
600
```
548
601
549
-
### Manual Cache Management
602
+
#### Manual Cache Management
550
603
551
604
```php
552
-
// Clear all expired cache entries
605
+
// Clear all expired cache entries (file storage only)
You can also manage cache using the CLI commands. See [CLI Commands](#cli-commands) section for details.
561
637
562
638
### Custom View Implementations
@@ -674,6 +750,165 @@ Recommendations:
674
750
Run: neuron mvc:cache:clear --expired
675
751
```
676
752
753
+
## Rate Limiting
754
+
755
+
The MVC component includes integrated rate limiting support through the routing component. Rate limiting helps protect your application from abuse and ensures fair resource usage.
756
+
757
+
### Configuration
758
+
759
+
Rate limiting is configured in your `config.yaml` file using two categories:
Configuration maps to environment variables using the `{category}_{name}` pattern:
788
+
- `RATE_LIMIT_ENABLED=true`
789
+
- `RATE_LIMIT_STORAGE=redis`
790
+
- `RATE_LIMIT_REQUESTS=100`
791
+
- `API_LIMIT_ENABLED=true`
792
+
- `API_LIMIT_REQUESTS=1000`
793
+
794
+
### Usage in Routes
795
+
796
+
#### Global Application
797
+
Set `global: true` in configuration to apply rate limiting to all routes:
798
+
```yaml
799
+
rate_limit:
800
+
enabled: true
801
+
global: true
802
+
requests: 100
803
+
window: 3600
804
+
```
805
+
806
+
#### Per-Route Application
807
+
Apply rate limiting to specific routes using the `filter` parameter in `routes.yaml`:
808
+
809
+
```yaml
810
+
routes:
811
+
# Public page - no rate limiting
812
+
- name: home
813
+
method: GET
814
+
route: /
815
+
controller: HomeController@index
816
+
817
+
# Standard protected route with rate limiting
818
+
- name: user_profile
819
+
method: GET
820
+
route: /user/profile
821
+
controller: UserController@profile
822
+
filter: rate_limit # Apply rate_limit (100/hour)
823
+
824
+
# API endpoint with higher limits
825
+
- name: api_users
826
+
method: GET
827
+
route: /api/users
828
+
controller: ApiController@users
829
+
filter: api_limit # Apply api_limit (1000/hour)
830
+
```
831
+
832
+
### Storage Backends
833
+
834
+
#### File Storage (Default)
835
+
Best for single-server deployments:
836
+
```yaml
837
+
rate_limit:
838
+
storage: file
839
+
file_path: cache/rate_limits # Directory for rate limit files
840
+
```
841
+
842
+
#### Redis Storage (Recommended for Production)
843
+
Best for distributed systems and high traffic:
844
+
```yaml
845
+
rate_limit:
846
+
storage: redis
847
+
redis_host: 127.0.0.1
848
+
redis_port: 6379
849
+
redis_database: 0
850
+
redis_prefix: rate_limit_
851
+
redis_auth: password # Optional
852
+
redis_persistent: true # Use persistent connections
853
+
```
854
+
855
+
#### Memory Storage (Testing Only)
856
+
For unit tests and development. Data is lost when PHP process ends:
857
+
```yaml
858
+
rate_limit:
859
+
storage: memory
860
+
```
861
+
862
+
### Rate Limit Headers
863
+
864
+
When rate limiting is active, the following headers are included in responses:
865
+
- `X-RateLimit-Limit`: Maximum requests allowed
866
+
- `X-RateLimit-Remaining`: Requests remaining in current window
867
+
- `X-RateLimit-Reset`: Unix timestamp when limit resets
868
+
869
+
When limit is exceeded (HTTP 429):
870
+
- `Retry-After`: Seconds until retry is allowed
871
+
872
+
### Example Implementation
873
+
874
+
1. Enable rate limiting in `config.yaml`:
875
+
```yaml
876
+
rate_limit:
877
+
enabled: true
878
+
global: false
879
+
storage: redis
880
+
requests: 100
881
+
window: 3600
882
+
redis_host: 127.0.0.1
883
+
884
+
api_limit:
885
+
enabled: true
886
+
storage: redis
887
+
requests: 1000
888
+
window: 3600
889
+
redis_host: 127.0.0.1
890
+
```
891
+
892
+
2. Apply to routes in `routes.yaml`:
893
+
```yaml
894
+
routes:
895
+
- name: login
896
+
method: POST
897
+
route: /auth/login
898
+
controller: AuthController@login
899
+
filter: rate_limit # Strict limit for login attempts
900
+
901
+
- name: api_data
902
+
method: GET
903
+
route: /api/data
904
+
controller: ApiController@getData
905
+
filter: api_limit # Higher limit for API access
906
+
```
907
+
908
+
### Customization
909
+
910
+
For advanced use cases, you can extend the rate limiting system by creating custom filters in your application. The rate limiting system automatically detects if the routing component version supports it and gracefully degrades if not available.
0 commit comments