Litewire 1.1.0 · HTTP

Request methods

Try each live example. Litewire shows a spinner and “Loading...” during every request, and temporarily replaces the initiating button content with its own loading spinner.

Litewire directive values are literal endpoint paths, not Zap route() calls. The demo endpoints return server-rendered fragments from app/Views/fragments/. Submitted data updates the displayed fragment for the current request; these demos do not persist data.

Node.js example setup

Node tabs use Express and Multer. Litewire sends ordinary form submissions as FormData, so Multer parses the POST example; express.json() parses the PUT and PATCH examples.

npm install express multer
import express from "express";
import multer from "multer";

const app = express();
const formData = multer();

app.use(express.json());

const text = (value) => typeof value === "string" ? value.trim() : "";
const escapeHtml = (value) =>
 String(value).replace(/[&<>"']/g, (character) =>
 ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", "\"": "&quot;", "'": "&#39;" }[character])
 );

The PHP tabs show the Zap handlers used by the live demos. Node handlers return equivalent HTML; values are escaped before insertion. Real applications should also validate, authorize, and persist changes as appropriate.

Zap controller input helper used by the PHP tabs

private function textInput(string $key): string
{
 $value = $this->request->input($key, '');
 if (!is_string($value)) {
 throw new \InvalidArgumentException("Expected a text value for {$key}.");
 }

 return trim($value);
}

GETRead data

Use lw-get to fetch content. If the trigger is inside a form, Litewire sends the form values as query parameters. This form asks the server for a rendered search-result fragment.

Live example

Submit the form to load results.

Markup

<form lw-get="/examples/http/search" lw-target="#get-results">
 <label for="get-query">Search term</label>
 <input id="get-query" name="q" value="litewire">
 <button type="submit">Search</button>
</form>

<div id="get-results">...</div>

Server-rendered fragment

<div class="alert alert-info mb-0" role="status">
 Search results for <strong>{{ $query }}</strong>: two example records found.
</div>

Server handler

Route::get('/examples/http/search', 'HttpController@searchExample');

public function searchExample(): Response
{
 return $this->view('fragments.http.search-results', data: [
 'query' => $this->textInput('q'),
 ]);
}
app.get("/examples/http/search", (req, res) => {
 const query = escapeHtml(text(req.query.q));
 res.type("html").send(`<div class="alert alert-info mb-0" role="status">
 Search results for <strong>${query}</strong>: two example records found.
</div>`);
});

POSTCreate data

Use lw-post to submit new data. Litewire sends the nearest form as FormData. The endpoint returns a rendered message fragment, which prepend inserts at the start of the list.

Live example

  • Existing example message

Markup

<form
 lw-post="/examples/http/messages"
 lw-target="#post-messages"
 lw-swap="prepend"
>
 <input name="message" value="Hello from POST" required>
 <button type="submit">Add message</button>
</form>

<ul id="post-messages">...</ul>

Server-rendered fragment

<li class="list-group-item">
 <span class="badge text-bg-success me-2">Created</span>{{ $message }}
</li>

Server handler

Route::post('/examples/http/messages', 'HttpController@createMessageExample');

public function createMessageExample(): Response
{
 return $this->view('fragments.http.message-item', data: [
 'message' => $this->textInput('message'),
 ]);
}
app.post("/examples/http/messages", formData.none(), (req, res) => {
 const message = escapeHtml(text(req.body.message));
 res.type("html").send(`<li class="list-group-item">
 <span class="badge text-bg-success me-2">Created</span>${message}
</li>`);
});

PUTReplace a resource

Use lw-put to send the full set of editable profile fields. With lw-json, Litewire serializes the form as JSON. The endpoint renders a replacement profile fragment, and outerHTML replaces the current profile.

Live example

Maya Chen

maya@example.test

Product designer and trail runner.

Markup

<form
 lw-put="/examples/http/profiles/42"
 lw-json
 lw-target="#put-profile"
 lw-swap="outerHTML"
>
 <input name="display_name" value="Maya Chen">
 <input name="email" type="email" value="maya@example.test">
 <textarea name="bio">Product designer and trail runner.</textarea>
 <button type="submit">Replace profile</button>
</form>

<section id="put-profile">...</section>

Server-rendered fragment

<section class="content-section mb-0" id="put-profile" aria-label="Updated profile">
 <h3 class="h6">{{ $displayName }}</h3>
 <p class="small mb-1">{{ $email }}</p>
 <p class="small mb-0">{{ $bio }}</p>
</section>

Server handler

Route::put('/examples/http/profiles/42', 'HttpController@replaceProfileExample');

public function replaceProfileExample(): Response
{
 return $this->view('fragments.http.profile', data: [
 'displayName' => $this->textInput('display_name'),
 'email' => $this->textInput('email'),
 'bio' => $this->textInput('bio'),
 ]);
}
app.put("/examples/http/profiles/42", (req, res) => {
 const displayName = escapeHtml(text(req.body.display_name));
 const email = escapeHtml(text(req.body.email));
 const bio = escapeHtml(text(req.body.bio));
 res.type("html").send(`<section class="content-section mb-0" id="put-profile" aria-label="Updated profile">
 <h3 class="h6">${displayName}</h3>
 <p class="small mb-1">${email}</p>
 <p class="small mb-0">${bio}</p>
</section>`);
});

PATCHUpdate part of a resource

Use lw-patch when submitting only the fields that changed. With lw-json, Litewire sends the form fields as JSON; the returned fragment replaces the task preview.

Live example

Review request examples

Needs review

Markup

<form
 lw-patch="/examples/http/tasks/42"
 lw-json
 lw-target="#patch-task"
 lw-swap="outerHTML"
>
 <input name="title" value="Review request examples">
 <button type="submit">Update task</button>
</form>

<section id="patch-task">...</section>

Server-rendered fragment

<section class="content-section mb-0" id="patch-task" aria-label="Updated task">
 <h3 class="h6 mb-1">{{ $title }}</h3>
 <span class="badge text-bg-primary">Updated</span>
</section>

Server handler

Route::patch('/examples/http/tasks/42', 'HttpController@updateTaskExample');

public function updateTaskExample(): Response
{
 return $this->view('fragments.http.task', data: [
 'title' => $this->textInput('title'),
 ]);
}
app.patch("/examples/http/tasks/42", (req, res) => {
 const title = escapeHtml(text(req.body.title));
 res.type("html").send(`<section class="content-section mb-0" id="patch-task" aria-label="Updated task">
 <h3 class="h6 mb-1">${title}</h3>
 <span class="badge text-bg-primary">Updated</span>
</section>`);
});

DELETERemove a resource

Use lw-delete to request deletion. The demo endpoint responds with no content; lw-swap="delete" removes the matching item after the successful response. Refresh the page to restore it.

Live example

Example message to remove

Markup

<article id="delete-message">
 <span>Example message to remove</span>
 <button
 type="button"
 lw-delete="/examples/http/messages/42"
 lw-target="#delete-message"
 lw-swap="delete"
 >
 Delete
 </button>
</article>

Server handler

Route::delete('/examples/http/messages/42', 'HttpController@deleteMessageExample');

public function deleteMessageExample(): Response
{
 return $this->html('', 204);
}
app.delete("/examples/http/messages/42", (req, res) => {
 res.status(204).end();
});

The live demo returns 204 so Litewire removes the item from the page; it does not persist a deletion.