mkastoun/laravel-419-handler
最新稳定版本:v1.1.2
Composer 安装命令:
composer require mkastoun/laravel-419-handler
包简介
Graceful handling of Laravel 419 session expired errors.
README 文档
README
Gracefully handle 419 Page Expired errors in Laravel using a clean, package-based trait that integrates into your Handler.php.
✨ Features
- Handles
TokenMismatchException(CSRF/session expiration) - Clean
trait-based integration (no overriding core Laravel handlers) - Redirects with flash messages (web)
- JSON error response (API)
- Configurable behavior
📦 Installation
Via Composer:
composer require mkastoun/laravel-419-handler
⚙️ Publish Configuration
php artisan vendor:publish --provider="Laravel419Handler\Laravel419HandlerServiceProvider" --tag=config
Configuration (config/laravel419.php)
return [ 'redirect_on_web' => '/', 'flash_message' => 'Your session has expired. Please try again.', 'auto_refresh_on_back' => true, 'json_response' => [ 'message' => 'Session expired. Please try again.', 'status' => 419, ], ];
🧩 Integration
In your App\Exceptions\Handler.php:
- Import and use the trait:
use Laravel419Handler\Traits\HandlesTokenMismatch; use Illuminate\Session\TokenMismatchException; class Handler extends ExceptionHandler { use HandlesTokenMismatch; public function render($request, Throwable $e) { if ($e instanceof TokenMismatchException) { return $this->handleTokenMismatch($request, $e); } return parent::render($request, $e); } }
- In your Blade layout, show the flash error (Optional):
@if(session('error')) <div class="alert alert-danger"> {{ session('error') }} </div> @endif
🧪 Testing
composer test
📄 License
MIT
🤝 Contributing
PRs welcome! Please submit issues, ideas, and improvements to help others benefit from this package.
🧠 Why Not Middleware?
While catching 419s via middleware is sometimes possible, it’s not 100% reliable because TokenMismatchException is thrown before controller or middleware logic in some cases. Using a trait inside the exception handler guarantees full coverage — safely and cleanly.
统计信息
- 总下载量: 99
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-26