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.
isHyper(): boolHow it works:
Checks for the Datastar-Request header that Datastar automatically sends with all AJAX requests.
Usage:
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:
public function show(Product $product)
{
$data = ['product' => $product];
return hyper()
->fragment('products.show', 'product-details', $data)
->web(view('products.show', $data));
}Common use cases:
// 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.
signals(string $key = null, mixed $default = null): mixed|\Dancycodes\Hyper\Http\HyperSignalParameters:
$key- Signal name (optional)$default- Default value if signal doesn't exist (optional)
Returns:
- If
$keyis provided: The signal value or default - If
$keyis null:HyperSignalinstance for method chaining
Get single signal:
public function update()
{
$name = request()->signals('name');
$email = request()->signals('email', 'default@example.com');
}Get HyperSignal instance:
public function store()
{
$validated = request()->signals()->validate([
'email' => 'required|email',
'password' => 'required|min:8'
]);
User::create($validated);
}Equivalent to signals() helper:
// These are identical:
request()->signals('count');
signals('count');
request()->signals()->all();
signals()->all();
request()->signals()->validate($rules);
signals()->validate($rules);Navigation Detection
isHyperNavigate()
Check if the request is a client-side navigation request.
isHyperNavigate(string|array|null $key = null): boolParameters:
$key- Navigation key(s) to check for (optional)null- Check if any navigation requeststring- Check for specific navigation keyarray- Check if any of the provided keys match
Returns: true if navigation request matches criteria, false otherwise
Check for any navigation:
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:
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:
if (request()->isHyperNavigate(['header', 'footer'])) {
// Update either header or footer
}How it works:
- Checks for
HYPER-NAVIGATEheader - If
$keyis provided, checksHYPER-NAVIGATE-KEYheader for matching key(s)
Headers sent by frontend:
HYPER-NAVIGATE: true
HYPER-NAVIGATE-KEY: sidebarhyperNavigateKey()
Get the navigation key from the request.
hyperNavigateKey(): ?stringReturns: Navigation key string or null if not a navigate request
Usage:
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:
$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.
hyperNavigateKeys(): arrayReturns: Array of navigation key strings, or empty array if not a navigate request
Usage:
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:
<!-- 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,chartRelated Documentation
- Signals Helper - Server-side signal manipulation
- HyperResponse - Response building methods
- Blade Directives - Blade integration
- Navigation - Client-side navigation guide

