About this example : REST Service Demo
Controller
MyRest.php
<?php
/**
* Class MyRest.
*
* A basic REST service responding to client GET and POST requests by using methods overriding
* of httpGetRequest and httpPostRequest from the base class \framework\RestService
*
* @package controllers
* @category Application Controller
* @author Rosario Carvello - rosario.carvello@gmail.com
*/
namespace controllers;
use framework\RestService;
use models\beans\BeanPart;
use models\beans\BeanPartCategory;
class MyRest extends RestService
{
public $bean;
public function __construct()
{
parent::__construct();
$this->grantRole(100);
$this->restrictToRBAC();
$this->allowMethod("part");
$this->allowMethod("category");
$this->addCORS("https://www.allowedomain.domain");
}
/**
* @override httpGetRequest
* @param $method
* @param $args
* @return array
*
*/
public function httpGetRequest($method, $args)
{
$custom = array();
if (!empty($method) && !empty($args)) {
switch ($method) {
case "part":
$this->bean = new BeanPart();
$this->bean->select($args[0]);
$custom = array("custom" => $this->bean->getDescription());
break;
case "category":
$this->bean = new BeanPartCategory();
$this->bean->select($args[0]);
$custom = array("custom" => $this->bean->getName());
}
}
return $custom;
}
/**
* override httpPostRequest
* @param $method
* @param $args
* @return array|string[]
*/
public function httpPostRequest($method, $args)
{
$custom = array();
if (!empty($method) && !empty($args)) {
switch ($method) {
case "part":
$this->bean = new BeanPart();
$custom = array("custom" => "POST on table part");
break;
case "category":
$this->bean = new BeanPartCategory();
$custom = array("custom" => "POST on table part_category");
break;
}
}
return $custom;
}
/**
* @param $method
* @param $args
* @return array
*/
public function httpPutRequest($method, $args)
{
return parent::httpPutRequest($method, $args); // TODO
}
/**
* @param $method
* @param $args
* @return array
*/
public function httpDeleteRequest($method, $args)
{
return parent::httpDeleteRequest($method, $args); // TODO
}
}
Model
none
View
none
HTML Template
my_rest_gui.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"
media="screen">
<title>Test REST Service</title>
</head>
<body>
<div class="container">
<h1>Rest service tester</h1>
<h2>You need <a href="../webmvc/common/login">admin</a> grants to call Rest Service</h2>
<hr>
This is a simple web interface for testing a custom REST service controller <strong>\controllers\MyRest.php</strong>
which extends <a href="examples/about/example/rest_service"><strong>framework\RestService</strong></a> class <br>
<hr>
To create PUT,GET,POST and DELETE rest service simple create a new controller by extending <strong>framework\RestService</strong>
by overriding the following methods <br><br><br>
<blockquote>
public function httpGetRequest($method, $args)
</blockquote>
<blockquote>
public function httpPostRequest($method, $args)
</blockquote>
<blockquote>
public function httpPutRequest($method, $args)
</blockquote>
<blockquote>
public function httpDeleteRequest($method, $args)
</blockquote>
<hr>
GET requests:
<ul>
<li><a href="my_rest/part/02"> my_rest/part/02</a></li>
<li><a href="my_rest/category/02"> my_rest/category/02</a></li>
</ul>
POST request:
<form class="form-inline">
<select id="method" class="form-control">
<option value="part">part</option>
<option value="category">category</option>
</select>
<select id="arg" class="form-control">
<option value="01">01</option>
<option value="02">02</option>
</select>
<a id="send" class="btn btn-primary" onclick="submitFormAjax()">Send POST request to service</a>
</form>
<hr>
<a href="examples/about/example/myrest" class="btn btn-info">Show source code</a>
<a href="examples/" class="btn btn-primary">Examples TOC</a>
</div>
<script>
function submitFormAjax() {
let xmlhttp= window.XMLHttpRequest ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200)
alert(this.responseText); // Here is the response
}
let method = document.getElementById('method').value;
let arg = document.getElementById('arg').value;
xmlhttp.open("POST","my_rest/"+ method +"/" + arg,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send();
}
</script>
</body>
</html>
Back