PHP code example of monkeyscloud / monkeyslegion-live
1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-live 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/ */
monkeyscloud / monkeyslegion-live example snippets
declare(strict_types=1);
namespace App\Live;
use MonkeysLegion\Live\LiveComponent;
use MonkeysLegion\Live\Attributes\State;
use MonkeysLegion\Live\Attributes\Action;
use MonkeysLegion\Live\Attributes\Computed;
final class Counter extends LiveComponent
{
#[State] public int $count = 0;
#[State] public int $step = 1;
#[Action]
public function increment(): void
{
$this->count += $this->step;
}
#[Action]
public function decrement(): void
{
$this->count -= $this->step;
}
#[Computed]
public function parity(): string
{
return $this->count % 2 === 0 ? 'even' : 'odd';
}
public function render(): string
{
return $this->view('live.counter');
}
}
#[State] public string $name = '';
#[State(persist: true)] public string $theme = 'light'; // survives navigation
#[State(url: true)] public string $tab = 'overview'; // synced to query string
#[State(readonly: true)] public int $userId; // signed, never accepted back
#[State(defer: true)] public array $data = []; // lazy-fetched
#[Action]
public function save(): void { /* ... */ }
#[Action(confirm: 'Delete permanently?')]
public function delete(int $id): void { /* ... */ }
#[Action(renderless: true)]
public function trackClick(): void { /* ... */ } // skip re-render
#[Computed]
public function fullName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
#[Computed(cache: true)]
public function expensiveReport(): array { /* ... */ }
public function mount(int $postId): void {} // once, on initial load
public function hydrate(): void {} // every request, after snapshot restored
public function dehydrate(): void {} // every request, before snapshot sent
public function updating(string $prop, $value): void {}
public function updated(string $prop, $value): void {}
public function rendering(): void {}
public function rendered(string $html): void {}
$this->emit('saved', $id); // to all components
$this->emitUp('child:done'); // to parent only
$this->emitSelf('refresh'); // to self only
$this->emitTo(Sidebar::class, 'sync'); // to specific component
$this->dispatchBrowser('confetti', ['count' => 50]); // browser CustomEvent
use MonkeysLegion\Live\Concerns\WithValidation;
final class RegisterForm extends LiveComponent
{
use WithValidation;
#[State] public string $email = '';
#[State] public string $password = '';
protected function rules(): array
{
return [
'email' => '
use MonkeysLegion\Live\Concerns\Streams;
final class AiChat extends LiveComponent
{
use Streams;
#[State] public array $messages = [];
#[State] public string $prompt = '';
#[Action]
public function send(Apex $apex): void
{
$this->messages[] = ['role' => 'user', 'content' => $this->prompt];
$this->prompt = '';
$this->stream('reply', function (StreamTarget $out) use ($apex) {
foreach ($apex->pipeline('chat')->stream($this->messages) as $token) {
$out->append($token);
}
});
}
}
use MonkeysLegion\Live\Testing\LiveTest;
final class CounterTest extends TestCase
{
use LiveTest;
public function test_increments(): void
{
Live::test(Counter::class)
->assertSee('0')
->set('step', 5)
->call('increment')
->assertSet('count', 5);
}
public function test_validation(): void
{
Live::test(RegisterForm::class)
->set('email', 'not-an-email')
->call('register')
->assertHasErrors(['email'])
->assertNoRedirect();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.