PHP code example of atk4 / api

1. Go to this page and download the library: Download atk4/api 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/ */

    

atk4 / api example snippets


$app->rest('/client/:/order_payments/::Orders::Payments', new Client($db));
 php
pi = new \Atk4\Api\Api();

// Simple handling of GET request through a callback.
$api->get('/ping', function() {
   return 'Pong';
});

// Methods can accept arguments, and everything is type-safe.
$api->get('/hello/:name', function ($name) {
    return "Hello, $name";
});
 php
$api->rest('/countries', new Country($db));
 php
$api->rest('/countries', function() use($db) {
  $c = new Country($db));
  $c->addCondition('is_eu', true);
  $c->setLimit(20);
  return $c;
});
 php
p = new \Atk4\Api\Api();

$db = \Atk4\Data\Persistence::connect($DSN);

// Lets set our index page
$app->get('/', function() {
    return 'This worked!';
});

// Getting access to POST data
$app->post('/stats/:id', function($id, $data) {
   return ['Received POST', 'id'=>$id, 'post_data'=>$data]
});
 php
$app->get('/:method', function($method) {
    // do something
});

$app->get('/ping', function() {
    return 'pong';
});
 php
function get($route, $action) {
    if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    	return $this->match($route, $action);
    }
}
 php
if ($app->match('/misc/**')) {
    // .. execute logic for requests starting with /misc/...
} else {
    // .. other logic
}
 php
$app->rest('/clients', new Client($db));
 php
$app->rest('/country/:iso_name');
 php
$app->rest('/clients/:id/orders/::Orders:id', new Client($db));
 php
$app->rest('/clients/:id/order_payments/::Orders::Payments:id', new Client($db));
 php
$app->rest('/client/:id/invoices-due', function($id) use($db) {
    $client = new Client($db);
    $client->load($id);
    return $client->ref('Invoices')->addCondition('status', 'due');
});
 php
// Enable user/password authentication. Field values are optional
$app->userAuth('/**', new User($db));
 php
$app->authUser('/**', new User($db));
$app->rest('/notifications', $app->user->ref('Notifications'));
 php
$app->authUser('/**', new User($db));

$limit = new \Atk4\Api\Limit($db);
$limit->addCondition('user_id', $app->user->id);

$app->get('/limits', function() use ($limit){
    return $limit;
});

$app->rateLimit('/**', $limit, 10);  // 10 requests per minute

$app->rest('/notifications', $app->user->ref('Notifications'));
 php
$cache = \Atk4\Data\Persistence\MemCache($conn);
$limit = new \Atk4\Api\Limit($cache);
 php
$audit_id = $app->auditLog(
  '/**',
  new \Atk4\Audit\Controller(
    new \Atk4\Audit\Model\AuditLog($db)
  )
);
 php
$app->auditLog->load($audit_id)->undo();
 php
$app->?
 php
$user_id = $app->authUser('/**', new User($db));

$db->addHook('afterAdd', function($o, $e) use ($user_id) {
    if ($e->hasElement('user_id')) {
        $e->addCondition('user_id', $user_id);
    }
})

 php
$app->map('/:resource/**', function(resource) use($app) {

    // convert user-credit to UserCredit
    $class = preg_replace('/[^a-zA-Z]/', '', ucwords($resoprce));

  	$object = $app->factory($class, null, 'Interface'); // Interface\UserCredit.php

  	return [$object, $app->method];
    // convert path to file
    // load file
    // create class instance
    // call method of that class

    // TODO: think of some logical example here!!
});
 php
function args(\Atk4\Data\Model $m) {
    if ($_GET['sort']) {
        $m->sortBy($_GET['sort']);
    }

    if ($_GET['condition']) {
    	foreach($_GET['condition'] as $key=>$val) {
            $m->addCondition($key, $val);
        }
    }

    if ($_GET['limit'] || $_GET['skip']) {
        $m->setLimit($_GET['limit']?:null, $_GET['skip']?:null);
    }

    // etc. etc...
}
 php
$app = new \Atk4\Ui\App\Api();

$app->group('/user/**', function($app2) {
   $app2->get('/test', function() {
     return 'yes';
   });
});