main/build/api/module/RESTexample.php

131 lines
3.0 KiB
PHP
Executable File

<?php
namespace api\module;
use error\core\Error;
use error\core\Err;
class RESTexample{
public function __construct(){
// Routine to execute before each call to RESTexample's method
}
public function __destruct(){
// Routine to execute after each call to RESTexample's method
}
public function POST_article($argv){
extract($argv);
// POST a new article with variables:
$title; // new article's title
$content; // new article's content
// ...
// process to create article and get $output_created_id
$success = true;
// ...
if( !$success )
return ['error' => new Error(Err::ModuleError)]; // or other `Err` constant
return [
'created_id' => "Article with title = `$title` and content=`$content`"
];
}
public function GET_article($argv){
extract($argv);
// GET all/an article with the variable:
$URL_0; // id of article ; if not given -> null
// ...
// process to get articles and get $output_get_articles
$success = true;
// ...
if( !$success )
return ['error' => new Error(Err::ModuleError)]; // or other `Err` constant
// optional param not given is set to null
if( is_null($URL_0) )
return ['articles' => [ "Article number `1`: sometitle / somecontent", "Article number `2`: sometitle / somecontent"] ];
else
return ['articles' => [ "Article number `$URL_0`: sometitle / somecontent"] ];
}
public function VIEW_article($argv){
extract($argv);
// VIEW a specific article (download json file) with the variable:
$URL_0; // id of article ; if not given -> null
// ...
// process to get articles and get $output_get_articles
$success = true;
// ...
if( !$success )
return ['error' => new Error(Err::ModuleError)]; // or other `Err` constant
// will download, but if AJAX will give a `link` to the file
return [
'headers' => [
'Content-Type' => 'application/json; charset=utf-8',
'Content-Disposition' => 'attachment; filename=export'.date('_d_m_Y', time()).'.json',
'Pragma' => 'no-cache',
'Expires' => '0'
],
'body' => "Article number `$URL_0`: sometitle / somecontent"
];
}
public function PUT_article($argv){
extract($argv);
// UPDATE an article with new content with variables:
$URL_0; // id of article to update
$title; // new article's title
$content; // new article's content
// ...
// process to get $output_updated_article
$success = true;
// ...
if( !$success )
return ['error' => new Error(Err::ModuleError)]; // or other `Err` constant
return ['article' => "Article number `$URL_0`: $title / $content"];
}
public function DELETE_article($argv){
extract($argv);
// DELETEs an article with the variable:
$URL_0; // id of the article to remove
// ...
// process to delete article
$success = true;
// ...
if( !$success )
return ['error' => new Error(Err::ModuleError)]; // or other `Err` constant
return ['log' => "Article `$URL_0` successfully deleted"]; // returns success
}
}