← Back to Home

Code Examples

Real snippets to get started with SiroPHP fast.

CRUD API in 2 Seconds

# Generate complete products API
php siro make:crud products

# Files created automatically:
# - Model, Controller, Migration, Routes, Tests

# Start server
php siro serve

# Test it
curl http://localhost:8080/api/products

JWT Authentication

# Generate full auth system
php siro make:auth

# Available endpoints:
# POST /auth/register
# POST /auth/login
# POST /auth/refresh
# POST /auth/logout
# GET  /auth/me

# Login and save token
php siro api:test POST /auth/login \
  email=admin@test.com \
  password=secret \
  --as=admin

Define Routes with Validation

// routes/api.php
Route::get('/products', [ProductController::class, 'index']);
Route::post('/products', [ProductController::class, 'store']);
Route::get('/products/{id}', [ProductController::class, 'show']);
Route::put('/products/{id}', [ProductController::class, 'update']);
Route::delete('/products/{id}', [ProductController::class, 'destroy']);

// With validation middleware
Route::post('/products', [ProductController::class, 'store'])
    ->middleware([AuthMiddleware::class, ThrottleMiddleware::class]);

Request Validation

public function store(Request $request)
{
    $validated = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|email|unique:users,email',
        'price' => 'required|numeric|min:0',
        'status' => 'in:active,inactive,pending',
    ]);

    $product = Product::create($validated);
    return Response::json($product, 201);
}

Debug Production Request

# Every response includes X-Siro-Trace-Id
curl -v https://api.example.com/users
# X-Siro-Trace-Id: siro_a1b2c3d4

# Replay exact production request
php siro replay a1b2c3d4

# Fix with watch mode
php siro fix

# Verify fix
php siro replay --diff

# Full trace with SQL queries
php siro log:trace a1b2c3d4

Model with Relationships

final class Post extends Model
{
    protected string $table = 'posts';

    protected array $fillable = [
        'title', 'body', 'user_id', 'status',
    ];

    public function author(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class, 'post_id');
    }
}

// Eager load
$post = Post::with('author', 'comments')->find(1);