ascorbic / use-php

Use PHP in React

Home Page:https://use-php.netlify.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

a blue Lambourghini with the licence "USEPHP"

🏎️ usePHP 💨

A React hook for running PHP? Why not?

It runs in a Netlify Edge Function and uses php-wasm to execute the PHP. It runs asynchronously, so you need a renderer that supports async components.

View the demo

deploy to netlify

Usage

/** @jsx h */
import { h, renderToString } from "https://deno.land/x/jsx/mod.ts";
import { usePHP } from "https://deno.land/x/use_php/mod.ts";

export default async function handler(request: Request) {
  const php = await usePHP(request, h);
  return new Response(
    await renderToString(
      <html>
        <head>
          <title>Use PHP</title>
        </head>
        <body>
          {await php`
              <?php phpinfo(); ?>
          `}
        </body>
      </html>
    ),
    {
      headers: {
        "content-type": "text/html; charset=UTF-8",
      },
    }
  );
}

export const config = {
  path: "/*",
  excluded_paths: "/public/*",
};

API

usePHP(request: Request, renderer?: JSXRenderer)

Pass in the request object and an optional JSX renderer and it will return tagged template that you can use to execute PHP. If you pass a JSX render function to the hook (e.g. h or React.createElement), then the template will return a JSX element. If you omit the renderer it will return an HTML string instead.

Try it yourself:

deploy to netlify

FAQ

  • Why?
    • Why not? That Lambo won't buy itself.
  • How can I deploy this to production?
    • 🤦🏻‍♂️

LICENCE

MIT Licence. © 2023 Matt Kane

About

Use PHP in React

https://use-php.netlify.app


Languages

Language:TypeScript 100.0%