API
Prisma PHP provides a simple and user-friendly way to interact with your database. With Prisma PHP, you can easily query your database, create, update, and delete records, and perform other database operations with just a few lines of code. Prisma PHP makes it easy to work with your database, allowing you to focus on building your application instead of writing complex SQL queries.
You can use any front-end framework or library to interact with your API. For example, you can use popular JavaScript frameworks like React, Angular, or Vue.js to build a dynamic and responsive front-end application that communicates with your Prisma PHP API. Additionally, you can leverage mobile app development frameworks like React Native or Flutter to create mobile applications that seamlessly integrate with your Prisma PHP API.
OpenAPI / Swagger Auto-generated
If you choose to work with Swagger Docs, Prisma PHP generates an OpenAPI 3 spec out of the box from your schema.prisma
and the routes created by the generator.
The JSON lives at:
./src/app/swagger-docs/apis/pphp-swagger.json
Use in React + TypeScript (Zod)
Example using openapi-zod-client
to generate Zod schemas and types from your spec:
# 1) Install generator
npm i -D openapi-zod-client
# 2) Generate client & schemas (adjust URL/port to your server)
npx openapi-zod-client \
--input http://localhost:3000/swagger-docs/apis/pphp-swagger.json \
--output ./client/src/api/pphp.ts
# 3) Use in React
// client/src/features/products/useProducts.ts
import { client, schemas } from "@/api/pphp"; // generated
export async function fetchProducts() {
const res = await client.GET("/products"); // typed path
// Validate safely with Zod:
const parsed = schemas.Product.array().safeParse(res.data);
if (!parsed.success) throw new Error("Invalid API response");
return parsed.data;
}
Prefer a different stack? You can also use openapi-typescript
, orval
, zodios
, or language-specific generators.
Useful Links for API Client Generation:
These tools help you generate strongly-typed API clients and schemas for your frontend or mobile apps, improving type safety and developer experience.
To create your endpoint, follow these steps:
β Would you like to create a backend-only project? β¦ No / Yes
- SelectYes
to create a backend-only project and install Prisma PHP.- Prisma PHP uses
route.php
as the default file for routing. This means that if you have a file namedroute.php
in a directory, it will be used as the endpoint for that directory. - Prisma PHP backend-only configuration file comes with a default
src/app/route.php
file. this is the default endpoint for your project. - Create a folder with the desired name, such as
products
, inside thesrc/app
directory. This is where you will create your endpoint using Prisma PHP's file-based routing. - Inside the created folder, create a file named
route.php
. - In the
route.php
file, define the logic for your endpoint and retrieve data from the database. - For creating a new route go to Routing documentation.
- For more information about the
route.php
file, you can refer to the Route File documentation. - For more information about the Prisma PHP ORM and getting started with setting up your database connection, you can refer to the
Prisma PHP ORM/Database Setup
documentation.
Project Structure for better organization:
.your-project-name // Your project root directory
βββ .server // Prisma PHP backend-only project directory
βββ .client // Your front-end project directory (React, Angular, Vue.js, etc.)
β βββ // navbar
β βββ // content
β β°ββ // footer
β°ββ...
CORS Configuration & Frontend Styling
# Single or multiple origins (CSV or JSON array)
CORS_ALLOWED_ORIGINS=[]
# If you need cookies/Authorization across origins, keep this true
CORS_ALLOW_CREDENTIALS="true"
# Optional tuning
CORS_ALLOWED_METHODS="GET,POST,PUT,PATCH,DELETE,OPTIONS"
CORS_ALLOWED_HEADERS="Content-Type,Authorization,X-Requested-With"
CORS_EXPOSE_HEADERS=""
CORS_MAX_AGE="86400"
Note: If you are using a local development server (such as Vite) for your frontend, make sure to add its URL to CORS_ALLOWED_ORIGINS
to prevent CORS errors.
Example Usage
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$products = $prisma->product->findMany();
echo json_encode($products);
Note: The $prisma
variable is an instance of the Prisma class, which is used to interact with the database. The $prisma
variable provides access to the Prisma PHP query builder, which allows you to query the database and perform other database operations. For more information about the Prisma PHP query builder, you can see the Query Builder documentation.
Example Usage Received Data from API
use Lib\Prisma\Classes\Prisma;
$prisma = Prisma::getInstance();
$productName = $params->productName;
$products = $prisma->product->findMany([
'where' => [
'name' => [
'contains' => $productName
]
]
]);
echo json_encode($products);
Note: The Request::$params
variable is a static property of the Request
class that contains the request parameters, such as those from GET, POST, PUT, DELETE, etc. methods. You can access individual parameters like Request::$params->id
. This makes it easy to retrieve data from the request and use it in your query. For more information about the Request::$params
variable, see the Params documentation.