1. Go to this page and download the library: Download rds/jrf 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/ */
rds / jrf example snippets
use \jrf\JRF;
$service = new JRF();
$service->controller()->handleMethod('method',
function($param_1, $param_2, JRF $app, $request_id) {
return $param_1 + $param_2
});
$service->listen();
use jrf\http\Input;
class MyInput extends Input
{
private $input;
public function __construct($input)
{
$this->input = $input;
}
public function read()
{
return $this->input;
}
}
// ..... service init
use MyInput;
$input = new MyInput($_POST['json']);
$service->request()->setInputProvider($input);
use \jrf\middleware\Base;
use \jrf\fault\ServerError;
class AccessMiddleware extends Base
{
private $ip_list=[];
public function __construct(array $ip_list) {
$this->ip_list = $ip_list;
}
public function call()
{
$info = $this->app->request()->info();
$ip = $info['ip_address'];
if (!in_array($ip, $this->ip_list)) {
ServerError::raise('access error');
}
$this->next->call();
}
}
// ... service init
use AccessMiddleware;
$list_allowed_ip = ['127.0.0.1'];
$service->add(new AccessMiddleware($list_allowed_ip));
use jrf\controller\Base;
use jrf\fault\MethodNotFound;
class MyControllerFactory extends Base
{
public function runMethodWithArgs($method, array $args =[], $request_id=0)
{
// fall-back if want to use closure-based methods
if ($this->isHandledMethod($method)) {
return parent::runMethodWithArgs($method, $args, $request_id);
}
// now try to create action class
if (strpos('.', $method)===false) {
MethodNotFound::raise();
}
list($group, $action) = explode('.', $method);
$class_name = sprint_f("\my\action\namespace\%s\%s", strtolower($group), ucfirst($action));
if (!class_exists($class_name)) {
MethodNotFound::raise();
}
$action = new $class_name($this->app());
if (!is_callable([$action, 'run'])) {
MethodNotFound::raise();
}
return $action->run();
}
}
// .... service init
use MyControllerFactory;
$factory = new MyControllerFactory;
$service->controller($factory);