niclas-van-eyk/laravel-transactional-controllers
Composer 安装命令:
composer require niclas-van-eyk/laravel-transactional-controllers
包简介
Effortlessly wrap your controller actions with database transactions.
README 文档
README
Effortlessly wrap your controller actions with database transactions.
class ExampleUsageController { #[Transactional] public function demo(Request $request) { User::create($request->all()); User::create($request->all()); throw new Exception("Everything will be rolled back!"); } }
Installation
You can install the package via composer:
composer require niclas-van-eyk/laravel-transactional-controllers
Background
If you want to make a series of edits to your database, where either all should happen at once, or none at all, you typically use database transactions. The example we use here is a user ($author) transferring a certain $amount of to another user ($receiver). We also want to save that the fact that this transfer took place in a separate model (TransferLog).
Usage
Before you might have written something like this:
namespace App\Http\Controllers; use App\Http\Requests\TransferMoneyRequest; use App\Models\TransferLog; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class BankAccountController { public function transferMoney(TransferMoneyRequest $request) { return DB::transaction(function () use ($request) { $request->author->balance->decrement($request->amount); $request->receiver->balance->increment($request->amount); return TransferLog::createFromTransferRequest($request); }) } }
You have to wrap your whole code inside one big closure, explicitly use all parameters you inject, and if you want to return something from inside the transaction closure, you end up with this double return, making the code harder to read and your IDE angry.
laravel-transactional-controllers solves this, by eliminating the need to wrap the code inside a closure and instead adding the Transactional attribute to the controller method:
namespace App\Http\Controllers; use App\Http\Requests\TransferMoneyRequest; use App\Models\TransferLog; use Illuminate\Http\Request; use NiclasVanEyk\TransactionalControllers\Transactional; // <-- from this package class BankAccountController { #[Transactional] public function transferMoney(TransferMoneyRequest $request): TransferLog { $request->author->balance->decrement($request->amount); $request->receiver->balance->increment($request->amount); return TransferLog::createFromTransferRequest($request); } }
No more use, double returns or your IDE complaining about it not being able to guarantee a correct return type!
You can also explicitly specify the database connection to use for running the transaction (config('database.default') is used by default):
#[Transactional(connection: 'other')] public function store() {}
Limitations
This only works when using controllers:
use NiclasVanEyk\TransactionalControllers\Transactional; // Works ✅ class RegularController { #[Transactional] public function store() {} } Route::post('/regular-store', [RegularController::class, 'store']); // Works ✅ class InvokableController { #[Transactional] public function __invoke() {} } Route::post('/invokable-store', InvokableController::class); // Does not work ❌ Route::post( '/invokable-store', #[Transactional] function () { /* Will not open a transaction! */}, )
Implementation Details
This package uses Laravels ControllerDispatcher component, which determines how the controller action should be executed. This means we can defer opening a transaction until the last possible moment, preventing unnecessary transactions from being opened! If e.g. the validation inside a FormRequest fails, or a model is not found when using route model binding, no transaction is started.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
If you have any ideas for changes, feel free to open issues, PRs or fork the project.
Local Development
This assumes you already have installed sqlite, PHP, and all composer dependencies locally.
Run tests
composer test
Run formatter
composer fix-cs
Run analysis
composer analyse
Run all of the above at once
composer ci
Credits
License
The MIT License (MIT). Please see License File for more information.
niclas-van-eyk/laravel-transactional-controllers 适用场景与选型建议
niclas-van-eyk/laravel-transactional-controllers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.12k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2022 年 05 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「laravel」 「transactions」 「niclas-van-eyk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 niclas-van-eyk/laravel-transactional-controllers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 niclas-van-eyk/laravel-transactional-controllers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 niclas-van-eyk/laravel-transactional-controllers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A PSR-7 compatible library for making CRUD API endpoints
A Laravel package to easily integrate Xendit payment gateway. It supports credit and debit cards, and e-wallet payments and custom invoices, queued notifications, webhook listeners and more.
Advanced Redis cache-driver with in-memory store, transactions and preloading
统计信息
- 总下载量: 1.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-05-21