Direct Function Invocation
Bypass the boilerplate. Call PHP backend functions directly from your frontend JavaScript using pp.fetchFunction.
Zero Boilerplate
No routers. No controllers. No route definitions. Just define a function and call it.
Server Actions
Treat your PHP functions as direct server actions, keeping logic centralized and secure.
Auto Parsing
Prisma handles the JSON serialization automatically. Arguments arrive as objects.
<?php use PP\Validator; // Callable via frontend function validateUser($data) { $id = Validator::int($data->id); if ($id <= 0) { return [ 'success' => false, 'msg' => 'Invalid ID' ]; } return [ 'success' => true, 'msg' => 'Valid ID' ]; } ?>
<script> const [id, setId] = pp.state(''); const [msg, setMsg] = pp.state(''); async function checkId() { // Magic happens here: const response = await pp.fetchFunction( 'validateUser', { id } ); if (response.success) { setMsg(response.msg); } } </script> <!-- UI --> <input value="{id}" oninput="setId(val)" /> <button onclick="checkId()">Check</button>
Technical Specification
The function returns a Promise that resolves to the PHP return value (automatically parsed as JSON) or a raw string if the backend returns non-JSON data.
fetchFunction<T = any>( functionName: string, data: Record<string, any> = {}, abortPrevious: boolean = false ): Promise<T | string>
Parameters
-
functionNameThe exact name of the PHP function to call. Can be a namespaced static method (e.g.,User::save) or a global function. -
dataAn object containing the arguments to pass to the function. These are mapped to the function parameters on the server side. -
abortPreviousIftrue, any pending requests initiated by this specific caller will be cancelled. Ideal for "search-as-you-type" inputs.
Advanced Capabilities
Automatic File Handling
You don't need FormData. If any value in your data object is a File or FileList, Prisma automatically switches to multipart/form-data.
const fileInput = document.getElementById('avatar');
await pp.fetchFunction('uploadAvatar', {
image: fileInput.files[0],
userId: 12
});
Encryption & headers
Function names are encrypted on the wire using AES-CBC to prevent enumeration attacks. Every request includes the HTTP_PP_WIRE_REQUEST header, allowing your backend to strictly validate request origin.
Request Cancellation
Prevent race conditions in search bars. Using abortPrevious: true automatically aborts stale requests.
// Typing "Apple" quickly triggers 5 keystrokes.
// Only the last request will reach the server.
pp.fetchFunction('search', { query: val }, true);
Transparent Redirects
If your PHP function returns a redirect instruction (or if authentication fails), fetchFunction catches it and performs a client-side navigation automatically via pp.redirect().