1. Go to this page and download the library: Download jacwright/restserver 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/ */
jacwright / restserver example snippets
class TestController
{
/**
* Returns a JSON string object to the browser when hitting the root of the domain
*
* @url GET /
*/
public function test()
{
return "Hello World";
}
/**
* Logs in a user with the given username and password POSTed. Though true
* REST doesn't believe in sessions, it is often desirable for an AJAX server.
*
* @url POST /login
*/
public function login()
{
$username = $_POST['username'];
$password = $_POST['password'];
// validate input and log the user in
}
/**
* Gets the user by id or current user
*
* @url GET /users/$id
* @url GET /users/current
*/
public function getUser($id = null)
{
if ($id) {
$user = User::load($id); // possible user loading method
} else {
$user = $_SESSION['user'];
}
return $user; // serializes object into JSON
}
/**
* Saves a user to the database
*
* @url POST /users
* @url PUT /users/$id
*/
public function saveUser($id = null, $data)
{
// ... validate $data properties such as $data->username, $data->firstName, etc.
$data->id = $id;
$user = User::saveUser($data); // saving the user to the database
return $user; // returning the updated or newly created user object
}
/**
* Gets user list
*
* @url GET /users
*/
public function listUsers($query)
{
$users = array('Andra Combes', 'Valerie Shirkey', 'Manda Douse', 'Nobuko Fisch', 'Roger Hevey');
if (isset($query['search'])) {
$users = preg_grep("/$query[search]/i", $users);
}
return $users; // serializes object into JSON
}
}
spl_autoload_register(); // don't load our classes unless we use them
$mode = 'debug'; // 'debug' or 'production'
$server = new RestServer($mode);
// $server->refreshCache(); // uncomment momentarily to clear the cache if classes change in production mode
$server->addClass('TestController');
$server->addClass('ProductsController', '/products'); // adds this as a base to all the URLs in this class
$server->handle();
/**
*
* >useCors = true;
$server->allowedOrigin = 'http://example.com';
// or use array of multiple origins
$server->allowedOrigin = array('http://example.com', 'https://example.com');
// or a wildcard
$server->allowedOrigin = '*';
/**
* Gets the user by id or current user
*
* @url GET /users/$id
* @url GET /users/current
*/
public function getUser($id = null)
{
if ($id) {
$user = User::load($id); // possible user loading method
if (!$user) {
throw new RestException(404, 'User not found');
}
} else {
$user = $_SESSION['user'];
}
return $user; // serializes object into JSON
}