home / skills / hoangnguyen0403 / agent-skills-standard / sessions-middleware
This skill enforces Laravel sessions and middleware best practices, promoting secure, scalable request handling with standardized headers and robust CSRF
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill sessions-middlewareReview the files below or copy the command above to add this skill to your agents.
---
name: Laravel Sessions & Middleware
description: Expert standards for session drivers, security headers, and middleware logic.
metadata:
labels: [laravel, session, middleware, security, high-density]
triggers:
files: ['app/Http/Middleware/**/*.php', 'config/session.php']
keywords: [session, driver, handle, headers, csrf]
---
# Laravel Sessions & Middleware
## **Priority: P1 (HIGH)**
## Structure
```text
app/Http/
├── Middleware/ # Custom logic layers
└── Kernel.php # Global/Group registration
```
## Implementation Guidelines
- **Session Driver**: Use `redis` or `memcached` for production/high-density environments.
- **Middleware Chain**: Keep logic granular; one middleware per responsibility.
- **Global Middleware**: Apply via `bootstrap/app.php` only for true globals (logging, headers).
- **Security Headers**: Standardize headers (HSTS, CSP, X-Frame) via dedicated middleware.
- **CSRF Protection**: Ensure `VerifyCsrfToken` is active for all web routes.
- **Session Lifecycle**: Use `$request->session()->regenerate()` after login/privilege changes.
## Anti-Patterns
- **File Streams**: **No file session driver**: Avoid in scaled apps due to I/O locks.
- **Env direct**: **No env('SESSION\_...')**: Always use `config('session...')`.
- **Heavy Bloat**: **No heavy logic in Middleware**: Offload to Services if >10 lines.
- **Trusting Client**: **No sensitive data in Cookies**: Store in server sessions only.
## References
- [Advanced Middleware Patterns](references/implementation.md)
This skill defines expert standards for Laravel sessions and middleware, focusing on secure, scalable defaults for production apps. It prescribes session drivers, middleware structure, and concrete security header policies to reduce common risks. The guidance is practical and aimed at maintainable middleware chains and session lifecycle handling.
The skill inspects and enforces patterns around session driver selection, middleware responsibilities, and global registration. It recommends dedicated middleware for security headers and lightweight middleware functions that delegate complex logic to services. It also enforces session lifecycle actions like regenerating sessions after authentication events and ensures CSRF protection is active for web routes.
Why avoid the file session driver?
File sessions cause I/O locks and poor concurrency, making them unsuitable for scaled or high-density environments.
When should I use global middleware vs route middleware?
Use global middleware only for truly global concerns like logging or headers; use Kernel.php and route groups for per-route or per-group behaviors.
Is it okay to store user preferences in cookies?
Avoid sensitive data in cookies. Non-sensitive preferences may be stored, but prefer server-side sessions for anything private or security-related.