PHP code example of dancycodes / hyper

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

    

dancycodes / hyper example snippets


// Laravel controller with Hyper's helpers
public function increment()
{
    $count = signals('count', 0);
    return hyper()->signals(['count' => $count + 1]);
}

public function store()
{
    $validated = signals()->validate([
        'name' => 'return hyper()->signals([
        'name' => '',
        'email' => '',
    ]);
}

public function destroy(User $user)
{
    $user->delete();

    $users = User::all();

    return hyper()->view('users.list', compact('users'));
}

public function updateCart()
{
    // Get specific signal with default value
    $quantity = signals('quantity', 1);

    // Check if signal exists
    if (signals()->has('coupon')) {
        $discount = signals('coupon');
    }

    // Get all signals
    $allSignals = signals()->all();

    // Get only specific signals
    $data = signals()->only(['name', 'email']);
}

// Update single signal
return hyper()->signals(['count' => 5]);

// Update multiple signals
return hyper()->signals([
    'count' => 5,
    'message' => 'Updated!',
    'errors' => []
]);

// Chain with other operations
return hyper()
    ->signals(['count' => 5])
    ->view('status', $data)
    ->js('console.log("Done")');

public function store()
{
    $validated = signals()->validate([
        'title' => 'ers'
    ]);

    Post::create($validated);

    return hyper()->signals(['message' => 'Post created']);
}

signals()->validate(
    [
        'email' => '    [
        'email.unique' => 'This email is already registered.',
        'password.min' => 'Password must be at least :min characters.'
    ],
    [
        'email' => 'email address',
        'password' => 'password'
    ]
);

public function uploadAvatar()
{
    signals()->validate([
        'avatar' => '()->user()->update(['avatar' => $path]);

    return hyper()->signals(['avatar' => $path]);
}

'avatar' => 'b64dimensions:min_width=100,max_width=1000,ratio=16/9'

// Store with auto-generated filename
$path = signals()->store('avatar', 'avatars', 'public');

// Store and get public URL
$url = signals()->storeAsUrl('avatar', 'avatars', 'public');

// Store multiple files
$paths = signals()->storeMultiple([
    'avatar' => 'avatars',
    'banner' => 'banners'
], 'public');

public function destroy(Todo $todo)
{
    $todo->delete();
    $todos = Todo::all();

    return hyper()
        ->fragment('todos.index', 'todo-list', compact('todos'))
        ->fragment('todos.index', 'todo-count', compact('todos'));
}

return hyper()->fragment('todos.index', 'todo-list', compact('todos'), [
    'selector' => '#todo-list',
    'mode' => 'outer'  // Replace the element selected with all content
]);

// Navigate to a URL
return hyper()->navigate('/dashboard');

// Navigate with merged query parameters
return hyper()->navigateMerge('/products', ['category' => 'electronics']);

// Navigate keeping only specific parameters
return hyper()->navigateOnly('/products', ['search', 'sort']);

// Navigate excluding specific parameters
return hyper()->navigateExcept('/products', ['page']);

// Navigate with debounce
return hyper()->navigate('/search', ['debounce' => 300]);

// Standard Laravel redirect
return redirect('/dashboard');

// With flash data
return redirect('/dashboard')->with('success', 'User created');

public function liveMetrics()
{
    return hyper()->stream(function($hyper) {
        while (true) {
            $metrics = $this->getLatestMetrics();

            $hyper->signals(['metrics' => $metrics])
                  ->fragment('dashboard', 'metrics-card', $metrics)
                  ->send();

            sleep(5);
        }
    });
}

return [
    'route_discovery' => [
        'enabled' => true,
        'discover_controllers_in_directory' => [
            app_path('Http/Controllers'),
        ],
    ],
];

use Dancycodes\Hyper\Routing\Attributes\Route;

#[Route(middleware: 'web')]
class PostController extends Controller
{
    public function index()
    {
        // Auto-registered as GET /post
    }

    #[Route(method: 'post')]
    public function store()
    {
        // Auto-registered as POST /post/store
    }

    #[Route(method: 'delete', uri: '/posts/{post}')]
    public function destroy(Post $post)
    {
        // Custom URI with route model binding
    }
}

return hyper()
    ->signals(['count' => $count])
    ->when($count > 10, function($hyper) {
        return $hyper->js('showWarning()');
    })
    ->unless($errors->isEmpty(), function($hyper) use ($errors) {
        return $hyper->signals(['errors' => $errors]);
    });

// Execute once
return hyper()->js('console.log("Task complete")');

// Execute with auto-removal
return hyper()->js('showNotification()', ['autoRemove' => true]);

// Dispatch custom DOM events
return hyper()->dispatch('task:complete', ['taskId' => 123]);
bash
php artisan vendor:publish --tag=hyper-assets
blade
<!-- Your view -->
<div id="user-list">
    @foreach($users as $user)
        <div class="user-item">
            <span>{{ $user->name }}</span>
            <button data-on:click="@deletex('/users/{{ $user->id }}')">
                Delete
            </button>
        </div>
    @endforeach
</div>
blade
<div @signals(['count' => 0, 'message' => 'Hello'])>
    <!-- Creates count and message signals -->
</div>
blade
<div @signals($user)>
    <!-- Eloquent Model → converted via toArray() -->
</div>

<div @signals($contacts)>
    <!-- Collection → converted via toArray() -->
</div>

<div @signals($results)>
    <!-- Paginator → converted via toArray() -->
</div>