route.php
Bypass the standard view rendering to build APIs, handle form submissions, or manage complex logic. A route.php file acts as a direct entry point for a specific URL path.
Basic Usage
Place a route.php file in any directory (e.g., /app/users/). This automatically creates an endpoint at /users.
<?php
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
// Fetch all users
$users = $prisma->user->findMany();
// Return JSON response
header('Content-Type: application/json');
echo json_encode($users);
Accessed via: GET http://localhost:3000/users
The Request Helper
Prisma provides static helpers to inspect the incoming HTTP request.
| Property | Description |
|---|---|
| Request::$isGet | Returns true if method is GET. |
| Request::$isPost | Returns true if method is POST. |
| Request::$isPut | Returns true if method is PUT. |
| Request::$isDelete | Returns true if method is DELETE. |
| Request::$params | Unified array containing GET/POST data and JSON body payloads. |
| Request::$requestedWith | Value of HTTP_X_REQUESTED_WITH (useful for detecting AJAX). |
Method Restriction & Error Handling
Use the Boom library to handle errors and enforce HTTP methods.
<?php
use PP\Request;
use PP\Header\Boom;
use Lib\Prisma\Classes\Prisma;
// 1. Guard Clause: Only allow GET
if (!Request::$isGet) {
Boom::methodNotAllowed()->toResponse();
exit;
}
$prisma = Prisma::getInstance();
// 2. Safe Parameter Access
// Request::$params handles $_GET, $_POST, and php://input automatically
$userId = Request::$params['id'] ?? null;
if (!$userId) {
Boom::badRequest('Missing ID parameter')->toResponse();
exit;
}
// 3. Database Logic
$user = $prisma->user->findFirst([
'where' => ['id' => (int)$userId]
]);
if (!$user) {
Boom::notFound()->toResponse();
exit;
}
echo json_encode($user);
Unified Parameters
Stop using $_POST or json_decode(file_get_contents(...)).
Request::$params automatically parses JSON bodies and standard form data into a single, easy-to-use array.