Skip to content

Commit 2d39da0

Browse files
committed
Adds quick note about responses
1 parent e249bcd commit 2d39da0

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

api/response/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ return ajax()->js("/assets/js/widget.js");
7171
Load a StyleSheet with the response.
7272

7373
```php
74-
return ajax()->js("/assets/css/widget.css");
74+
return ajax()->css("/assets/css/widget.css");
7575
```
7676

7777
### Method - `img()` {#img}
7878

7979
Load an image with the response.
8080

8181
```php
82-
return ajax()->img("/assets/css/widget.css");
82+
return ajax()->img("/assets/images/logo.png");
8383
```
8484

8585
### Method - `browserEvent()` {#browserEvent}

index.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Larajax includes a small client-side layer that lets you call your server-side h
124124

125125
When a request is made, it is sent to the **current page URL** and the target handler is passed in the `X-AJAX-HANDLER` request header. This keeps all requests scoped to the active page route while avoiding the need for exposed API endpoints.
126126

127-
### Markup Tools
127+
### From Markup
128128

129129
The `data-request` attribute is the primary way to trigger an AJAX handler from HTML.
130130

@@ -150,7 +150,7 @@ When a request is triggered from inside a form, the form inputs are automaticall
150150
For a full list of supported attributes and behaviors, see the [AJAX Handlers Guide](./guide/ajax-handlers.md).
151151
:::
152152

153-
### JavaScript Tools
153+
### From JavaScript
154154

155155
When markup alone is not enough, the same handlers can be called directly from JavaScript using `jax.ajax()`.
156156

@@ -174,7 +174,39 @@ When you want to serialize the input values of a container or form explicitly, y
174174

175175
This gives you the same request model as data-request, but with full control over when and how the call is made.
176176

177-
178177
::: tip
179178
More details on working with JavaScript are available in the [JavaScript Guide](./guide/ajax-javascript.md).
180-
:::
179+
:::
180+
181+
### From the Controller
182+
183+
Larajax provides a global `ajax()` helper that returns an instance of the `Larajax\Classes\AjaxResponse` class. This response object lets you compose multiple instructions into a single server response and describe exactly how the browser should react.
184+
185+
Instead of returning raw JSON and handling everything manually on the client, you describe the outcome of the action directly in the controller.
186+
187+
```php
188+
function onSave()
189+
{
190+
return ajax()
191+
// Include some response data
192+
->data(['success' => true])
193+
194+
// Patch a DOM element
195+
->update(['#someElement' => 'Hello world!'])
196+
197+
// Load a JavaScript file
198+
->js('assets/js/app.js')
199+
200+
// Trigger a halting browser event
201+
->browserEventAsync('app:redirecting')
202+
203+
// Redirect the browser
204+
->redirect('https://larajax.org');
205+
}
206+
```
207+
208+
Each call adds another instruction to the response queue. When the request completes, the client-side runtime processes these instructions in order and applies the corresponding changes to the page. This keeps your controller in full control of both the server-side logic and the resulting UI behavior.
209+
210+
::: tip
211+
To explore the full set of available response operations, see the [AJAX Response Guide](./guide/ajax-responses.md).
212+
:::

0 commit comments

Comments
 (0)