1. Go to this page and download the library: Download litepie/actions library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
litepie / actions example snippets
namespace App\Actions;
use App\Models\User;
use Litepie\Actions\StandardAction;
class CreateUserAction extends StandardAction
{
protected function rules(): array
{
return [
'name' => 'this->data['name'],
'email' => $this->data['email'],
'password' => bcrypt($this->data['password']),
]);
}
}
use Litepie\Actions\BaseAction;
class SimpleAction extends BaseAction
{
protected function handle(): mixed
{
return ['message' => 'Action completed'];
}
}
use Litepie\Actions\StandardAction;
class StandardUserAction extends StandardAction
{
protected function rules(): array
{
return ['email' => '
use Litepie\Actions\CompleteAction;
class ComplexOrderAction extends CompleteAction
{
protected function rules(): array
{
return ['order_id' => '],
['action' => UpdateInventoryAction::class],
] : [];
}
protected function getNotifications(): array
{
return [
[
'recipients' => User::admins()->get(),
'class' => OrderProcessedNotification::class
]
];
}
protected function handle(): Order
{
$order = Order::find($this->data['order_id']);
$order->update(['status' => 'processed']);
return $order;
}
}
use Litepie\Actions\BaseAction;
use Litepie\Actions\Traits\AuthorizesActions;
use Litepie\Actions\Traits\LogsActions;
class CustomAction extends BaseAction
{
use AuthorizesActions, LogsActions;
protected function handle(): mixed
{
// Your custom logic
}
}
use Litepie\Actions\Conditional\ConditionalAction;
$result = ConditionalAction::make()
->when(fn($data) => $data['user_type'] === 'premium')
->then(CreatePremiumUserAction::class)
->otherwise(CreateRegularUserAction::class)
->with($userData)
->execute();
// Or using helper function
$result = action_when(fn($data) => $data['amount'] > 1000)
->then(ProcessLargeOrderAction::class)
->otherwise(ProcessRegularOrderAction::class)
->with($orderData)
->execute();
use Litepie\Actions\Middleware\LoggingMiddleware;
use Litepie\Actions\Middleware\RateLimitMiddleware;
// Built-in middleware
class MyAction extends BaseAction
{
// Middleware will be applied automatically in pipelines
}
// Custom middleware
class CustomMiddleware extends ActionMiddleware
{
public function handle(ActionContract $action, Closure $next): mixed
{
// Before action execution
$result = $next($action);
// After action execution
return $result;
}
}
// Enable caching for specific executions
$result = CreateUserAction::make($model, $data)
->cached('user-creation-' . $data['email'], 3600)
->execute();
// Cache with custom key and TTL
$result = ProcessReportAction::make()
->with($reportData)
->cached('report-' . $reportId, 7200)
->execute();
// Clear cached results
$action->clearCache();
// Disable caching for this execution
$result = $action->fresh()->execute();
use Litepie\Actions\Models\ActionLog;
// Query action logs
$logs = ActionLog::inLog('actions')
->where('action', 'CreateUser')
->where('created_at', '>=', now()->subDays(7))
->get();
// Get logs for specific model
$userLogs = ActionLog::forSubject($user)->get();
// Get logs by user
$adminLogs = ActionLog::causedBy($admin)->get();
// Get log properties
$log = ActionLog::first();
$executionTime = $log->getExtraProperty('execution_time');
$userAgent = $log->getExtraProperty('user_agent');