PHP code example of yuga / livecomponent

1. Go to this page and download the library: Download yuga/livecomponent 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/ */

    

yuga / livecomponent example snippets


return [
    'discovery' => [
        [
            'path' => path('app/Live'),
            'namespace' => 'App\\Live',
            'prefix' => null,
        ],
    ],

    'uploads' => [
        'maxSize' => 10 * 1024 * 1024,
        'types' => ['image/jpeg', 'image/png', 'application/pdf'],
    ],
];



namespace App\\Live;

use Yuga\Live\Attributes\Live;
use Yuga\Live\Component;

#[Live(name: 'counter')]
class Counter extends Component
{
    public int $count = 0;

    public function increment(): void
    {
        $this->count++;
    }

    public function render(): string
    {
        return <<<HTML
            <section>
                <strong>{$this->count}</strong>
                <button type="button" ylc:click="increment">+</button>
            </section>
        HTML;
    }
}

#[Live(
    name: 'orders',
    lazy: true,
    stream: true,
    streamInterval: 1000,
    poll: false,
)]

<?= ylc('counter') 

<?= ylc('orders', [$customerId], [
    'stream' => true,
    'streamInterval' => 2000,
    'placeholder' => '<p>Loading orders...</p>',
]) 

public function mount(int $customerId): void
{
    $this->customerId = $customerId;
}

use Yuga\Live\Form;

class ProfileForm extends Form
{
    public string $name = '';
    public string $email = '';

    public function rules(): array
    {
        return [
            'name' => '

public ProfileForm $form;

public function save(): void
{
    if (!$this->form->validate()) {
        return;
    }

    // Persist $this->form->all().
    $this->form->reset();
}

use Yuga\Live\Attributes\Url;

#[Url(as: 'q')]
public string $search = '';

#[Url(history: false)]
public int $page = 1;

use Yuga\Live\Attributes\Computed;

#[Computed]
public function getTotalProperty(): int
{
    return $this->items->sum('price');
}

public function boot(): void {}
public function mount(...$params): void {}
public function hydrate(): void {}
public function dehydrate(): void {}
public function updating(string $property, mixed $value): void {}
public function updated(string $property, mixed $value): void {}