PHP

From DreamFactory
Jump to: navigation, search
(Accessing Resources)
(Accessing Resources)
Line 5: Line 5:
 
DreamFactory passes in two [[DreamFactory/Features/Scripting#Resources Available To A Script|additional objects]] for use in the scripts.
 
DreamFactory passes in two [[DreamFactory/Features/Scripting#Resources Available To A Script|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.
 
In PHP, these resources are represented as native arrays and can be accessed as normal. See the examples below.
 
Using the '''platform''' resource...
 
<pre>
 
$payload = ['resource' => [['name' => 'test', 'complete' => false]]];
 
$result = $platform['api']->post->__invoke('mysql/_table/todo', $payload);
 
</pre>
 
  
 
Using the '''event''' resource...
 
Using the '''event''' resource...
Line 44: Line 38:
 
     'result' => $result
 
     'result' => $result
 
];
 
];
 +
</pre>
 +
 +
Using the '''platform''' resource...
 +
<pre>
 +
$payload = ['resource' => [['name' => 'test', 'complete' => false]]];
 +
$result = $platform['api']->post->__invoke('mysql/_table/todo', $payload);
 
</pre>
 
</pre>
  

Revision as of 00:10, 16 July 2016

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 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...

$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
];

Using the platform resource...

$payload = ['resource' => [['name' => 'test', 'complete' => false]]];
$result = $platform['api']->post->__invoke('mysql/_table/todo', $payload);

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.