PSR-15 middleware for CSRF protection in Slim Framework applications.
Slim-Csrf is a PSR-15 middleware component for the Slim Framework that adds Cross-Site Request Forgery (CSRF) protection to web applications. It generates and validates unique tokens for unsafe HTTP requests (like POST, PUT, DELETE, PATCH) to ensure that form submissions and API calls are legitimate and not forged by malicious actors.
PHP developers building applications with the Slim Framework who need to secure forms and API endpoints against CSRF attacks.
It provides a standardized, framework-native solution that integrates seamlessly with Slim's middleware architecture, offers flexibility in token management (per-request or per-session), and allows custom handling of validation failures.
Slim Framework CSRF protection middleware
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Implements the PHP-FIG PSR-15 middleware standard, ensuring interoperability with other middleware and adherence to modern PHP practices.
Supports both per-request and per-session token persistence, allowing developers to balance security with convenience for AJAX-heavy applications, as detailed in the Token Persistence section.
Provides direct methods like getTokenName() and getTokenValue() for easy exposure in templating engines such as Twig, reducing boilerplate in views.
Allows overriding the default 400 error response with custom logic via setFailureHandler(), enabling tailored user experiences on CSRF validation failures.
Only works with Slim 4.0.0 or newer, making it unsuitable for applications built on other PHP frameworks or custom setups without significant adaptation.
Requires additional code, such as creating a Twig extension, to integrate tokens into templates, which adds development overhead compared to drop-in solutions.
Per-session token mode depends on PHP sessions, which can be a limitation in stateless, distributed, or session-less environments like some API architectures.