PHP code example of stanx / extdirect

1. Go to this page and download the library: Download stanx/extdirect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stanx / extdirect example snippets



s Server
{
    public function date($format)
    {
        return date($format);
    }
}

ExtDirect::provide('Server', true);

ExtDirect::$configuration_name = $value;  

  ExtDirect::$api_classes = ['MyClass', 'AnotherClass'=>'MyNamespace\\AnotherClass'];

  ExtDirect::$api_classes_discovery_dirs = [__DIR__ . '/MyNamespace/MyAPI' => 'MyNamespace\\MyAPI'];

  ExtDirect::$url = '/path/to/my_php_script.php';  

  ExtDirect::$namespace = 'Ext.php'; 

  ExtDirect::$descriptor = 'Ext.php.REMOTING_API';  

  ExtDirect::$id = 'MyProvider';  

  ExtDirect::$max_retries = 1;  

  ExtDirect::$timeout = 30000;  

  ExtDirect::$count_only_

  ExtDirect::$

  ExtDirect::$

  ExtDirect::$instantiate_static = true;

  ExtDirect::$constructor_send_params = true;

  ExtDirect::$constructor_params = ['MyNamespace\\MyClass' => ['param1', 'param2']];

  ExtDirect::$debug = true;

  ExtDirect::$utf8_encode = true;  

  ExtDirect::$params_enforce_associative = true;  

  ExtDirect::$default_api_output = 'javascript';  

  ExtDirect::$form_handlers = ['someClass::someMethod', 'MyNamespace\\Server::date'];  

class FTP_Manager
{
    /**
    * Sets FTP password for a specific account
    * 
    * @extdirect-formHandler
    * @param string $account   Name of the account
    * @param string $password   New password
    * @param string $password_confirm   New password confirmation
    * @return string
    */
    public function set_ftp_password($account, $password, $password_confirm)
    {
        // do stuff
        return 'result';
    }
}  

ExtDirect::$form_handlers[] = 'FTP_Manager::set_ftp_password';  

function set_ftp_password($data){};
function do_something($data){};
function do_something_completely_different($data){};  

function set_ftp_password($account, $password, $password_confirm){}

// New configuration option
ExtDirect::$id = 'my_api';

// All "function" configurations accept parameters of callback type
ExtDirect::$declare_method_function     = 'declare_method';
ExtDirect::$authorization_function      = 'authorize';
ExtDirect::$instantiate_function        = 'instantiate';
ExtDirect::$transform_result_function   = 'transform_result';
ExtDirect::$transform_response_function = 'transform_response';

function declare_method(string $class, string $method) : bool
{
    // return boolean - declare the method in the API or not
    return in_array($class . '::' . $method, MyFramework::$user->permissions);
}

function authorize(ExtDirectAction $action, callable $callback, array $parameters) : bool
{
    // return boolean - authorize the action call or not
    return declare_method($action->class, $action->method);
}

function instantiate(ExtDirectAction $action) : object
{
    // return instance of class
    return DI::get($action->class);
}

function transform_result(ExtDirectAction $action, mixed $result) : mixed
{
    if ($action->form_handler)
        $result = ['success' => $result];

    // return modified result
    return $result;
}

function transform_response(ExtDirectAction $action, array $response) : array
{
    $response['error_msg']   = MyFramework::$errors;
    $response['success_msg'] = MyFramework::$success;

    // return modified response
    return $response;
}