几天内即可获得 Laravel 代码审查方面的专家指导

Chevere Workflow: A Declarative PHP Workflow Engine with Async Job Execution

发布日期 经过

Chevere Workflow: A Declarative PHP Workflow Engine with Async Job Execution image

When your application needs to run a sequence of steps — some dependent on each other, others that could run at the same time — it's tempting to stuff everything into a single class or chain method calls together. The Chevere Workflow package offers a cleaner approach: declare your jobs, wire up their dependencies, and let the engine automatically determine execution order.

安装

作曲家 要求 chevere/workflow

A Practical Example

Consider a user registration flow that needs to:

  • Create the user account
  • Send a welcome email
  • Set up a free trial subscription
  • Notify your team on Slack

Some of these steps depend on the account being created first, while others can run in parallel once it is. Managing this manually means tracking shared state, writing glue code, and handling partial failures yourself. Workflow handles all of that declaratively.

Building the Pipeline

Here is how you might model the onboarding flow above:

使用 功能 Chevere\Workflow\ { 工作流程 , 同步 , 异步 , 多变的 , 回复 , 跑步 };
$workflow = 工作流程
createAccount : 同步
CreateUserAccount ::班级 ,
姓名 : 多变的 '姓名' ),
电子邮件 : 多变的 '电子邮件' ),
密码 : 多变的 '密码' ),
),
sendWelcomeEmail : 异步
发送欢迎电子邮件 ::班级 ,
用户身份 : 回复 'createAccount' , 'ID' ),
电子邮件 : 回复 'createAccount' , '电子邮件' ),
),
createTrial : 同步
CreateTrialSubscription ::班级 ,
用户身份 : 回复 'createAccount' , 'ID' ),
-> withRunIf 多变的 'enableTrial' )),
notifyTeam : 异步
NotifySlackChannel ::班级 ,
userEmail : 回复 'createAccount' , '电子邮件' ),
),
(英文):

A few things worth noting here:

  • sync() blocks until the job completes; async() runs concurrently once its dependencies are met
  • variable('name') is a placeholder for a value you supply at runtime
  • response('createAccount', 'id') 引用 id field from createAccount 's output
  • ->withRunIf(variable('enableTrial')) makes the createTrial job conditional — it is skipped entirely if enableTrial is falsy

The engine builds a dependency graph from these declarations. Because sendWelcomeEmailnotifyTeam both depend only on createAccount , they run concurrently once the account is ready — without you needing to orchestrate that yourself.

Defining a Job

Each job can be a closure, an invocable class, or a class extending Chevere's Action . Here's a straightforward invocable:

使用 Chevere\Action\Action ;
班级 CreateUserAccount 延伸 行动
{
民众 功能 __invoke
细绳 $名称,
细绳 $email,
细绳 $password,
: 大批 {
$用户 = 用户 :: 创造 ([
'姓名' => $名称,
'电子邮件' => $email,
'密码' => 加密 ($password),
]);
返回 [
'ID' => $用户 -> ID,
'电子邮件' => $用户 -> 电子邮件,
];
}
}

The return value becomes the job's response. Downstream jobs reference individual fields via response('createAccount', 'id') or the whole response via response('createAccount')

Running the Workflow

Pass your variables directly to run() , and access any job's output from the result:

$结果 = 跑步
$workflow,
姓名 : “无名氏” ,
电子邮件 : 'jane@example.com' ,
密码 : '秘密' ,
enableTrial : 真的 ,
(英文):
用户 ID = $结果 -> 回复 'createAccount' -> 整数 'ID' (英文):

Handling Failures

If any job throws, Workflow wraps it in a WorkflowException that tells you exactly which job failed and why:

使用 Chevere\Workflow\Exceptions\WorkflowException ;
尝试 {
$结果 = 跑步 ($workflow, ... (英文):
} 抓住 WorkflowException $e) {
记录器 () -> 错误 "Job '{ $e -> 姓名 }' failed" ,[
'错误' => $e -> 可抛出 -> 获取消息 (),
]);
}

Retrying Unreliable Jobs

For jobs that call external services prone to occasional hiccups, attach a retry policy with withRetry() :

notifyTeam : 异步
NotifySlackChannel ::班级 ,
userEmail : 回复 'createAccount' , '电子邮件' ),
-> withRetry 暂停 : 60 , maxAttempts : 3 , 延迟 : 5 ),

Laravel 集成

A dedicated package brings first-class Laravel support, adding Artisan commands, an AbstractWorkflow base class, and a facade for running workflows anywhere in your app.

作曲家 要求 chevere/workflow-laravel

Generate a workflow class with Artisan:

php 工匠 制作:工作流 UserOnboarding

Then fill in the definition using the same jobs from earlier:

使用 Chevere\WorkflowLaravel\AbstractWorkflow ;
使用 Chevere\Workflow\Interfaces\WorkflowInterface ;
使用 功能 Chevere\Workflow\ { 工作流程 , 同步 , 异步 , 多变的 , 回复 };
班级 UserOnboarding 延伸 AbstractWorkflow
{
受保护 功能 定义 () : WorkflowInterface
{
返回 工作流程
createAccount : 同步
CreateUserAccount ::班级 ,
姓名 : 多变的 '姓名' ),
电子邮件 : 多变的 '电子邮件' ),
密码 : 多变的 '密码' ),
),
sendWelcomeEmail : 异步
发送欢迎电子邮件 ::班级 ,
用户身份 : 回复 'createAccount' , 'ID' ),
电子邮件 : 回复 'createAccount' , '电子邮件' ),
),
createTrial : 同步
CreateTrialSubscription ::班级 ,
用户身份 : 回复 'createAccount' , 'ID' ),
-> withRunIf 多变的 'enableTrial' )),
notifyTeam : 异步
NotifySlackChannel ::班级 ,
userEmail : 回复 'createAccount' , '电子邮件' ),
),
(英文):
}
}

Run it via the Workflow facade — from a controller, a job, or anywhere else:

使用 Chevere\WorkflowLaravel\Facades\Workflow ;
$结果 = 工作流 :: 跑步 UserOnboarding ::班级 ,
姓名 :$request -> 姓名,
电子邮件 :$request -> 电子邮件,
密码 :$request -> password,
enableTrial :$request -> 布尔值 'enable_trial' ),
(英文):
用户 ID = $结果 -> 回复 'createAccount' -> 整数 'ID' (英文):

Laravel's service container automatically handles constructor injection for class-based jobs, so any service your job needs can be type-hinted and resolved without extra configuration.

You can also list all registered workflows with php artisan workflow:list , or trigger one directly from the terminal with php artisan workflow:run

Visualising the Graph

If you use VS Code, the package has an official extension that renders your workflow as a Mermaid flowchart. The diagram shows each job as a node, draws edges between dependent jobs, and annotates those edges with the data flowing between them — so you can see at a glance that sendWelcomeEmail waits on createAccount , or that createTrial only runs when enableTrial is truthy. Because the graph is derived directly from your code, it stays accurate as you add or rearrange jobs with nothing to maintain separately.

包起来

Chevere Workflow provides structure for multi-step processes that would otherwise scatter logic across multiple classes or make a single method unwieldy. The declarative style makes it easy to see what runs when, and the automatic dependency graph means parallel execution happens with no extra configuration.

The package also ships with testing utilities so you can verify each job in isolation and assert that your workflow's execution graph matches your expectations — something that is much harder when steps are tightly coupled.

查看 Chevere Workflow repository on GitHub for the full feature set, including Action classes, Workflow Providers, and the VS Code extension.

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
了解软科技

以每小时 20 美元的价格聘请具备人工智能专业知识的 Laravel 开发人员。48 小时内即可开始工作。

访问 Acquaint Softtech
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Laravel Schema Sentinel: Detect and Fix Database Schema Drift image

Laravel Schema Sentinel: Detect and Fix Database Schema Drift

阅读文章
Laravel Web Push Notifications image

Laravel Web Push Notifications

阅读文章
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

阅读文章
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

阅读文章
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

阅读文章
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

阅读文章