定制 net-code/laravel-bus 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

net-code/laravel-bus

Composer 安装命令:

composer require net-code/laravel-bus

包简介

Attribute-routed command & query buses for Laravel with a configurable middleware pipeline.

README 文档

README

Attribute-routed command and query buses for Laravel, with a configurable middleware pipeline.

  • A command/query is a plain object carrying a #[HandledBy(Handler::class)] attribute.
  • The bus resolves the named handler from the container and invokes it (__invoke).
  • Handlers run inside a middleware pipeline you configure per bus.

Extracted from the net-tech portfolio API so the CQRS kernel can be shared across projects.

Install

composer require net-code/laravel-bus

The service provider is auto-discovered. It binds CommandBus, QueryBus and TransactionManager and wires the default middleware.

Usage

use NetCode\Bus\Command\Command;
use NetCode\Bus\Command\CommandHandler;
use NetCode\Bus\Command\HandledBy;

#[HandledBy(CreateUserHandler::class)]
final readonly class CreateUser implements Command
{
    public function __construct(public string $email) {}
}

final readonly class CreateUserHandler implements CommandHandler
{
    public function __invoke(CreateUser $command): string
    {
        // ...
        return $id;
    }
}
use NetCode\Bus\Command\CommandBus;

$id = app(CommandBus::class)->dispatch(new CreateUser('a@b.c'));

Queries are identical via NetCode\Bus\Query\* and QueryBus::ask().

Middleware

Ships with:

  • TransactionMiddleware (command bus) — wraps each handler in a DB transaction.
  • LoggingQueryMiddleware (query bus) — logs each dispatched query at debug level.

Both defaults are configurable. Publish the config to change them:

php artisan vendor:publish --tag=bus-config
// config/bus.php
return [
    'command' => ['middleware' => [
        NetCode\Bus\Laravel\TransactionMiddleware::class,
        App\Bus\AuthorizeMiddleware::class, // your own
    ]],
    'query' => ['middleware' => [
        NetCode\Bus\Laravel\LoggingQueryMiddleware::class,
    ]],
];

Middleware are resolved from the container (constructor injection works) and applied outermost-first. Custom middleware implement CommandMiddleware / QueryMiddleware.

Transactions

TransactionMiddleware depends on NetCode\Bus\TransactionManager, bound by default to LaravelTransactionManager (uses the default DB connection). Rebind it to change the transaction boundary.

License

MIT

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-07-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固