niclas-van-eyk/laravel-transactional-controllers 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

Latest Version on Packagist Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 niclas-van-eyk/laravel-transactional-controllers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.12k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-21