publicPHPRequestHandler
Index
Constructors
Properties
Accessors
Methods
Constructors
constructor
The request handler needs to decide whether to serve a static asset or run the PHP interpreter. For static assets it should just reuse the primary PHP even if there’s 50 concurrent requests to serve. However, for dynamic PHP requests, it needs to grab an available interpreter. Therefore, it cannot just accept PHP as an argument as serving requests requires access to ProcessManager.
Parameters
config: PHPRequestHandlerConfiguration
Request Handler configuration.
Returns PHPRequestHandler
Properties
getFileNotFoundAction
processManager
rewriteRules
Accessors
absoluteUrl
The absolute URL of this PHPRequestHandler instance.
Returns string
documentRoot
The directory in the PHP filesystem where the server will look for the files to serve. Default:
/var/www
.Returns string
Methods
getPrimaryPhp
Returns Promise<PHP>
internalUrlToPath
Converts an absolute URL based at the PHPRequestHandler to a relative path without the server pathname and scope.
Parameters
internalUrl: string
An absolute URL based at the PHPRequestHandler root.
Returns string
The relative path.
pathToInternalUrl
Converts a path to an absolute URL based at the PHPRequestHandler root.
Parameters
path: string
The server path to convert to an absolute URL.
Returns string
The absolute URL.
request
Serves the request – either by serving a static file, or by dispatching it to the PHP runtime.
The request() method mode behaves like a web server and only works if the PHP was initialized with a
requestHandler
option (which the online version of WordPress Playground does by default).In the request mode, you pass an object containing the request information (method, headers, body, etc.) and the path to the PHP file to run:
const php = PHP.load('7.4', {
requestHandler: {
documentRoot: "/www"
}
})
php.writeFile("/www/index.php", `<?php echo file_get_contents("php://input");`);
const result = await php.request({
method: "GET",
headers: {
"Content-Type": "text/plain"
},
body: "Hello world!",
path: "/www/index.php"
});
// result.text === "Hello world!"The
request()
method cannot be used in conjunction withcli()
.Parameters
request: PHPRequest
PHP Request data.
Returns Promise<PHPResponse>
Handles HTTP requests using PHP runtime as a backend.
Use PHPRequestHandler implicitly with a new PHP instance:
Explicitly create a PHPRequestHandler instance and run a PHP script: