PHP

From DreamFactory
Jump to: navigation, search

DreamFactory supports writing event scripts and custom scripting service scripts in PHP. Note: These scripts are not sandboxed like V8js so care must be taken to prevent unwanted access to system resources, e.g, the file system of the host server that is running DreamFactory. Please see our example scripts for PHP for more information. No additional software is required to run PHP scripts.

Accessing Resources

DreamFactory passes in two additional objects for use in the scripts. In PHP, these resources are represented as native arrays and can be accessed as normal. See the examples below.

Using the event resource...

$result = $platform['api']->get->__invoke('system/environment');

Using the platform resource...

$verb = $event['request']['method'];
 
if ($verb !== 'GET') {
    // currently always throws a 500 to client
    throw new \Exception('Only HTTP GET is allowed on this endpoint');
}
 
$params = $event['request']['parameters'];
$required = ['n1', 'n2'];
 
foreach ($required as $element) {
    if (!isset($params[$element])) {
        $event['response'] = [
            'status_code' => 400, 
            'content' => [
                'success' => false,
                'message' => "Required parameter $element not found in request."
            ]
        ];
        return;
    }
}
 
$result = $params['n1']+$params['n2'];
 
// status_code defaults to 200 
return [
    'success' => true,
    'result' => $result
];


Including Other Scripts

Currently, PHP scripts can not directly include other PHP code via require() methods or use statements as you normally would. However, DreamFactory can access any other code installed in the system by using fully qualified name-spaced class names.