Backend API Reference

Complete reference for all Laravel Gale backend methods, including the GaleResponse class, GaleRedirect class, and Request macros.

GaleResponse Methods

State Methods

Method Signature Description
state state(string|array $key, mixed $value = null, array $options = []) Set state value(s)
forget forget(string|array $keys) Remove state keys
messages messages(array $messages) Set messages state
clearMessages clearMessages() Clear all messages
// Single key-value
gale()->state('count', 42);

// Array of values
gale()->state(['name' => 'John', 'email' => 'john@example.com']);

// With onlyIfMissing option
gale()->state('defaults', $data, ['onlyIfMissing' => true]);

// Remove state
gale()->forget('temporaryData');
gale()->forget(['key1', 'key2']);

// Messages
gale()->messages(['email' => 'Invalid email']);
gale()->clearMessages();

View & Fragment Methods

Method Signature Description
view view(string $view, array $data = [], array $options = [], bool $web = false) Render view as HTML
fragment fragment(string $view, string $fragment, array $data = [], array $options = []) Render Blade fragment
fragments fragments(array $fragments) Render multiple fragments
// Full view
gale()->view('users.index', ['users' => $users]);

// Single fragment
gale()->fragment('users.index', 'user-list', ['users' => $users]);

// Multiple fragments
gale()->fragments([
    ['users.index', 'user-list', ['users' => $users]],
    ['users.index', 'pagination', ['users' => $users]],
]);

DOM Manipulation Methods

Method Signature Description
html html(string $html, array $options = [], bool $web = false) Morph HTML into page
append append(string $selector, string $html) Append to element
prepend prepend(string $selector, string $html) Prepend to element
before before(string $selector, string $html) Insert before element
after after(string $selector, string $html) Insert after element
inner inner(string $selector, string $html) Replace inner HTML
outer outer(string $selector, string $html) Replace outer HTML
replace replace(string $selector, string $html) Replace element
remove remove(string $selector) Remove element
Method Signature Description
navigate navigate(string|array $url, string $key = 'true', array $options = []) Navigate to URL
navigateWith navigateWith(string|array $url, string $key = 'true', bool $merge = false) Navigate with merge control
navigateMerge navigateMerge(string|array $url, string $key = 'true') Navigate merging params
navigateClean navigateClean(string|array $url, string $key = 'true') Navigate without merge
navigateOnly navigateOnly(string|array $url, array $only, string $key = 'true') Keep only specified params
navigateExcept navigateExcept(string|array $url, array $except, string $key = 'true') Remove specified params
navigateReplace navigateReplace(string|array $url, string $key = 'true') Replace history entry
updateQueries updateQueries(array $queries, string $key = 'filters') Update query params
clearQueries clearQueries(array $paramNames, string $key = 'clear') Clear query params
reload reload() Full page reload

Component Methods

Method Signature Description
componentState componentState(string $name, array $state, array $options = []) Update named component state
componentMethod componentMethod(string $name, string $method, array $args = []) Call method on component

Event & JavaScript Methods

Method Signature Description
dispatch dispatch(string $event, array $data = [], array $options = []) Dispatch DOM event
js js(string $script, array $options = []) Execute JavaScript

Conditional Methods

Method Signature Description
when when(mixed $condition, callable $true, ?callable $false = null) Execute if truthy
unless unless(mixed $condition, callable $callback) Execute if falsy
whenGale whenGale(callable $gale, ?callable $web = null) Execute for Gale requests
whenNotGale whenNotGale(callable $callback) Execute for non-Gale
whenGaleNavigate whenGaleNavigate(?string $key, callable $callback) Execute for navigate requests

Other Methods

Method Signature Description
redirect redirect(string $url) Create GaleRedirect
stream stream(callable $callback) Enable streaming mode
web web(mixed $response) Set web fallback
withEventId withEventId(string $id) Set SSE event ID
withRetry withRetry(int $ms) Set SSE retry interval
reset reset() Clear all events

GaleRedirect Methods

Method Signature Description
with with(string|array $key, mixed $value = null) Flash data to session
withInput withInput(?array $input = null) Flash input data
withErrors withErrors(mixed $errors) Flash validation errors
back back(string $fallback = '/') Redirect back
backOr backOr(string $route, array $params = []) Back with route fallback
refresh refresh(bool $query = true, bool $fragment = false) Refresh current page
home home() Redirect to root
route route(string $name, array $params = [], bool $absolute = true) Named route redirect
intended intended(string $default = '/') Auth intended redirect
forceReload forceReload(bool $bypass = false) Hard page reload

Request Macros

Macro Signature Description
isGale isGale() Check if Gale request
state state(?string $key = null, mixed $default = null) Get state from request
isGaleNavigate isGaleNavigate(string|array|null $key = null) Check if navigate request
galeNavigateKey galeNavigateKey() Get navigate key
galeNavigateKeys galeNavigateKeys() Get all navigate keys
validateState validateState(array $rules, array $messages = [], array $attrs = []) Validate request state
// Check request type
if (request()->isGale()) {
    return gale()->state('data', $data);
}

// Get state from request
$count = request()->state('count', 0);
$email = request()->state('user.email');

// Check navigate request
if (request()->isGaleNavigate('sidebar')) {
    return gale()->fragment('page', 'sidebar', $data);
}

// Validate state
$validated = request()->validateState([
    'email' => 'required|email',
    'name' => 'required|min:2',
]);

On this page