Skip to content

Request Macros

Laravel Hyper extends Laravel's Request object with macros that detect Hyper requests, access signals, and handle client-side navigation. These methods are available on both the request() helper and request instances.

Request Detection

isHyper()

Check if the current request is a Hyper (AJAX) request.

php
isHyper(): bool

How it works:

Checks for the Datastar-Request header that Datastar automatically sends with all AJAX requests.

Usage:

php
public function index()
{
    if (request()->isHyper()) {
        // Return fragment for AJAX request
        return hyper()->fragment('products.index', 'product-list', $data);
    }

    // Return full page for initial load
    return view('products.index', $data);
}

Progressive enhancement pattern:

php
public function show(Product $product)
{
    $data = ['product' => $product];

    return hyper()
        ->fragment('products.show', 'product-details', $data)
        ->web(view('products.show', $data));
}

Common use cases:

php
// Skip expensive operations for AJAX requests
if (!request()->isHyper()) {
    Log::info('Full page view loaded');
    $this->trackPageView();
}

// Different response formats
return request()->isHyper()
    ? hyper()->signals(['data' => $data])
    : response()->json(['data' => $data]);

Signal Access

signals()

Access signals from the current request.

php
signals(string $key = null, mixed $default = null): mixed|\Dancycodes\Hyper\Http\HyperSignal

Parameters:

  • $key - Signal name (optional)
  • $default - Default value if signal doesn't exist (optional)

Returns:

  • If $key is provided: The signal value or default
  • If $key is null: HyperSignal instance for method chaining

Get single signal:

php
public function update()
{
    $name = request()->signals('name');
    $email = request()->signals('email', 'default@example.com');
}

Get HyperSignal instance:

php
public function store()
{
    $validated = request()->signals()->validate([
        'email' => 'required|email',
        'password' => 'required|min:8'
    ]);

    User::create($validated);
}

Equivalent to signals() helper:

php
// These are identical:
request()->signals('count');
signals('count');

request()->signals()->all();
signals()->all();

request()->signals()->validate($rules);
signals()->validate($rules);

isHyperNavigate()

Check if the request is a client-side navigation request.

php
isHyperNavigate(string|array|null $key = null): bool

Parameters:

  • $key - Navigation key(s) to check for (optional)
    • null - Check if any navigation request
    • string - Check for specific navigation key
    • array - Check if any of the provided keys match

Returns: true if navigation request matches criteria, false otherwise

Check for any navigation:

php
public function index()
{
    if (request()->isHyperNavigate()) {
        // This is a navigation request
        return hyper()->fragment('layout', 'content', $data);
    }

    return view('dashboard', $data);
}

Check for specific navigation key:

php
public function dashboard()
{
    if (request()->isHyperNavigate('sidebar')) {
        return hyper()->fragment('layout', 'sidebar', $sidebarData);
    }

    if (request()->isHyperNavigate('main')) {
        return hyper()->fragment('layout', 'main', $mainData);
    }

    // Full page load
    return view('dashboard', $data);
}

Check for multiple keys:

php
if (request()->isHyperNavigate(['header', 'footer'])) {
    // Update either header or footer
}

How it works:

  1. Checks for HYPER-NAVIGATE header
  2. If $key is provided, checks HYPER-NAVIGATE-KEY header for matching key(s)

Headers sent by frontend:

HYPER-NAVIGATE: true
HYPER-NAVIGATE-KEY: sidebar

hyperNavigateKey()

Get the navigation key from the request.

php
hyperNavigateKey(): ?string

Returns: Navigation key string or null if not a navigate request

Usage:

php
public function page()
{
    $key = request()->hyperNavigateKey();

    if ($key === 'sidebar') {
        return hyper()->fragment('layout', 'sidebar', $data);
    }

    if ($key === 'content') {
        return hyper()->fragment('layout', 'content', $data);
    }

    return view('page', $data);
}

Switch statement:

php
$key = request()->hyperNavigateKey();

return match($key) {
    'sidebar' => hyper()->fragment('layout', 'sidebar', $sidebarData),
    'header' => hyper()->fragment('layout', 'header', $headerData),
    'footer' => hyper()->fragment('layout', 'footer', $footerData),
    default => view('layout', $data)
};

hyperNavigateKeys()

Get all navigation keys as an array.

php
hyperNavigateKeys(): array

Returns: Array of navigation key strings, or empty array if not a navigate request

Usage:

php
public function update()
{
    $keys = request()->hyperNavigateKeys();

    $response = hyper();

    foreach ($keys as $key) {
        match($key) {
            'stats' => $response->fragment('dashboard', 'stats', $statsData),
            'chart' => $response->fragment('dashboard', 'chart', $chartData),
            'activity' => $response->fragment('dashboard', 'activity', $activityData),
            default => null
        };
    }

    return $response;
}

Multiple keys from frontend:

blade
<!-- Triggers navigation with multiple keys -->
<a href="/dashboard"
   data-navigate__key.stats__key.chart="true">
    Update Dashboard
</a>

Headers sent:

HYPER-NAVIGATE: true
HYPER-NAVIGATE-KEY: stats,chart