PHP code example of abdullahozcan / liveigniter

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

    

abdullahozcan / liveigniter example snippets


$psr4 = [
    'LiveIgniter' => ROOTPATH . 'vendor/liveigniter/liveigniter/src',
];

$routes->group('', ['namespace' => 'LiveIgniter\Controllers'], function($routes) {
    



namespace App\LiveComponents;

use LiveIgniter\LiveComponent;

class Counter extends LiveComponent
{
    public int $count = 0;
    public string $message = 'Hello from LiveIgniter!';
    
    public function increment(): void
    {
        $this->count++;
    }
    
    public function decrement(): void
    {
        $this->count--;
    }
    
    public function reset(): void
    {
        $this->count = 0;
    }
}

<div id="<?= $componentId 

<!DOCTYPE html>
<html>
<head>
    <title>LiveIgniter Example</title>
    <script defer src="https://unpkg.com/[email protected]/dist/cdn.min.js"></script>
    <script src="/liveigniter/assets/js/liveigniter.js"></script>
</head>
<body>
    <?= live_component('App\LiveComponents\Counter') 

<!-- Component with auto-generated x-data -->
<div id="<?= $componentId 

// Emit an event
$this->emit('user.updated', $userId, $userData);

// Listen for events
$this->listen('user.updated', function($userId, $userData) {
    // Handle the event
});

class MyComponent extends LiveComponent
{
    public string $name = '';
    public int $age = 0;
    public array $items = [];
    public bool $isActive = false;
    public ?User $user = null;
}



namespace Config;

use LiveIgniter\Config\LiveIgniter as BaseLiveIgniter;

class LiveIgniter extends BaseLiveIgniter
{
    public bool $debug = true;
    public int $sessionLifetime = 3600;
    public string $assetsUrl = '/liveigniter/assets/';
    // ... other configuration options
}

class UserProfile extends LiveComponent
{
    public User $user;
    public string $name = '';
    public string $email = '';
    
    public function mount(User $user): void
    {
        $this->user = $user;
        $this->name = $user->name;
        $this->email = $user->email;
    }
    
    public function save(): void
    {
        $this->user->update([
            'name' => $this->name,
            'email' => $this->email
        ]);
        
        session()->setFlashdata('success', 'Profile updated!');
    }
    
    public function cancel(): void
    {
        $this->name = $this->user->name;
        $this->email = $this->user->email;
    }
}

class ChatComponent extends LiveComponent
{
    public array $messages = [];
    
    public function mount(): void
    {
        $this->refreshMessages();
        
        // Listen for new messages
        $this->listen('message.sent', [$this, 'onMessageSent']);
    }
    
    public function sendMessage(string $message): void
    {
        // Save message
        $messageModel = new MessageModel();
        $messageModel->save([
            'user_id' => auth()->id(),
            'message' => $message
        ]);
        
        // Emit to other components
        $this->emit('message.sent', $message);
    }
    
    public function onMessageSent(): void
    {
        $this->refreshMessages();
    }
    
    private function refreshMessages(): void
    {
        $this->messages = (new MessageModel())->findAll();
    }
}

public bool $debug = true;
bash
php spark liveigniter:install
bash
php spark liveigniter:publish
bash
# Install LiveIgniter (recommended for new projects)
php spark liveigniter:install

# Publish only assets
php spark liveigniter:publish --assets

# Publish only config files
php spark liveigniter:publish --config

# Publish example views
php spark liveigniter:publish --views