-
-
Notifications
You must be signed in to change notification settings - Fork 0
Installation & Setup
This guide will walk you through installing and configuring Laravel Statecraft in your Laravel application.
Laravel Statecraft requires:
- PHP: 8.1 or higher
- Laravel: 10.x or 11.x
- Composer: For package management
Install Laravel Statecraft using Composer:
composer require grazulex/laravel-statecraft
π‘ Auto-Discovery
The service provider will be automatically registered thanks to Laravel's package auto-discovery feature.
Publish the configuration file to customize Laravel Statecraft:
php artisan vendor:publish --tag=statecraft-config
This creates config/statecraft.php
with the default configuration.
If you want to track state history, publish and run the migrations:
php artisan vendor:publish --tag=statecraft-migrations
php artisan migrate
This creates the state_machine_histories
table for tracking state changes.
The configuration file config/statecraft.php
contains several important settings:
'state_machines_path' => resource_path('state-machines'),
Defines where your YAML state machine definitions are stored.
'enable_history' => true,
Enable or disable state change history tracking.
'cache' => [
'enabled' => true,
'ttl' => 3600, // 1 hour
'prefix' => 'statecraft',
],
Configure caching for improved performance.
'events' => [
'enabled' => true,
'broadcast' => false,
],
Control event firing and broadcasting.
After installation, create the following directory structure:
resources/
βββ state-machines/ # YAML state machine definitions
β βββ OrderStateMachine.yaml
β βββ UserStateMachine.yaml
β βββ ...
βββ ...
app/
βββ StateMachine/
β βββ Guards/ # Guard classes
β β βββ PaymentGuard.php
β β βββ ...
β βββ Actions/ # Action classes
β βββ ProcessPayment.php
β βββ ...
Use the Artisan command to generate your state machine:
php artisan statecraft:make OrderStateMachine --model=Order
This creates:
-
resources/state-machines/OrderStateMachine.yaml
- State machine definition - Example guard and action classes
Add the HasStateMachine
trait to your Eloquent model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Grazulex\LaravelStatecraft\Traits\HasStateMachine;
class Order extends Model
{
use HasStateMachine;
protected $stateMachine = 'OrderStateMachine';
protected $fillable = [
'amount',
'customer_email',
'status', // This will store the current state
];
}
Ensure your model's table has a column to store the current state:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->string('status')->default('pending')->after('amount');
});
}
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('status');
});
}
};
Test that everything is working correctly:
<?php
use App\Models\Order;
// Create an order
$order = Order::create([
'amount' => 100.00,
'customer_email' => '[email protected]'
]);
// Check initial state
echo $order->getCurrentState(); // Should output: pending
// Verify state machine is loaded
$stateMachine = $order->getStateMachine();
echo $stateMachine->getName(); // Should output: OrderStateMachine
State machine file not found
- Ensure your YAML file exists in
resources/state-machines/
- Check the filename matches your
$stateMachine
property
Model not found error
- Verify the model class exists and the namespace is correct in your YAML
Permission errors
- Ensure Laravel can write to the storage and cache directories
Cache issues
- Clear the application cache:
php artisan cache:clear
- Disable cache temporarily in config for debugging
Enable debug mode for additional logging:
// In config/statecraft.php
'debug' => env('STATECRAFT_DEBUG', false),
Set in your .env
file:
STATECRAFT_DEBUG=true
Now that Laravel Statecraft is installed and configured:
- Learn the Basics - Understand core concepts
- YAML Configuration - Master the YAML syntax
- Explore Examples - See real-world implementations
Having issues? Check our troubleshooting guide or create an issue on GitHub.
π― Laravel Statecraft - Advanced State Machine Implementation for Laravel
Navigate: π Home | π¦ Installation | π Basic Guide | π YAML Config | π‘ Examples
Resources: π‘οΈ Guards & Actions | π― Events | π State History | π§ͺ Testing | π¨ Commands
π Community Resources:
Made with β€οΈ for the Laravel community β’ Contribute β’ License