Appearance
System Variables in FlowBuilder
Overview
FlowBuilder supports two types of variables to enable data storage and sharing between nodes:
- Flow Variables: Created by users within a specific flow and only available in that flow. These are stored in the database.
- System Variables: Provided by modules and available to all flows. These are stored only in memory and are recreated each time the application starts.
The Variables Service
FlowBuilder provides a VariablesManager
class and a corresponding Variable
facade to make it easy to register and manage variables.
Registering System Variables
System variables are registered by creating a dedicated class for each variable, extending BaseVariable
.
Example: Creating a System Variable
Create a new class in app/Variables/
:
php
<?php
declare(strict_types=1);
namespace Modules\FlowBuilder\Variables;
use Modules\FlowBuilder\Support\BaseVariable;
class TodayVariable extends BaseVariable
{
protected string $name = 'Today';
protected string $key = 'today';
public function getValue(): \Closure
{
return fn () => now()->format('Y-m-d');
}
}
Registering the Variable
In your service provider, register the variable class using the Variable
facade:
php
use Modules\FlowBuilder\Facades\Variable;
use Modules\FlowBuilder\Variables\TodayVariable;
protected function registerFlowVariables(): void
{
Variable::register(TodayVariable::class);
// Register other variables...
}
Parameters
key
: Unique identifier for the variable (snake_case recommended)name
: Human-readable name for the UIgetValue()
: Returns a closure that resolves the value at runtime
Node Variables
Node variables are specific variables that exist only when the current node is present in the flow. These variables allow you to capture and use context-specific data from the node within the node's settings and logic.
You can define node variables by adding a variables
property to the node class:
php
// Example of a trigger when a form is submitted
class FormSubmissionTrigger extends BaseNode
{
//... other node properties and methods
protected ?array $variables = [
UserPhoneNumberVariable::class,
UserEmailVariable::class,
UserNameVariable::class,
];
}
Creating Node Variables
All node variables should extend the BaseNodeVariable class, similar to system variables. This base class allows you to define the variable's unique key, display name, and the logic to retrieve its value if needed.
php
// Example of a node variable
class UserPhoneNumberVariable extends BaseNodeVariable
{
protected string $name = 'User Phone Number';
protected string $key = 'user_phone_number';
}
Best Practices
- Use snake_case for variable keys
- Prefix with your module name to avoid conflicts (e.g.,
whatsapp_phone_number
) - Keep keys concise but descriptive
- Use clear, human-readable names
- Use modern PHP features and proper typing
Dynamic Values in System Variables
The getValue()
method should return a closure. This closure can accept contextual parameters, such as Flow
and FlowProcess
, and can use dependency injection if needed:
php
public function getValue(): \Closure
{
return function (?Flow $flow = null, ?FlowProcess $flowProcess = null) {
// Your logic here
};
}
Example: Company Name Variable
php
class CompanyNameVariable extends BaseVariable
{
protected string $name = 'Company Name';
protected string $key = 'company_name';
public function getValue(): \Closure
{
return function (?Flow $flow = null, ?FlowProcess $process = null) {
if ($flow && $flow->tenant) {
return $flow->tenant->name;
}
if ($process && $process->flow && $process->flow->tenant) {
return $process->flow->tenant->name;
}
return tenant()->name;
};
}
}
Using Variables in Templates
Within node settings, users can reference variables using the syntax . The system will automatically replace these references with the actual variable values during execution.
For example, in an HTTP node, the user can define a URL that includes a variable:
https://api.example.com/customers/{{customer_id}}/orders
During execution, if customer_id
has the value 12345
, the URL will be resolved as:
https://api.example.com/customers/12345/orders