1. Go to this page and download the library: Download galancev/php-rest-service 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/ */
galancev / php-rest-service example snippets
use RestService\Server;
Server::create('/')
->addGetRoute('test', function(){
return 'Yay!';
})
->addGetRoute('foo/(.*)', function($bar){
return $bar;
})
->addPostRoute('foo', function($field1, $field2) {
// do stuff with $field1, $field2 etc
// or you can directly get them with $_POST['field1']
})
->run();
use RestService\Server;
Server::create('/admin', 'myRestApi\Admin')
->collectRoutes()
->run();
namespace MyRestApi;
class Admin {
/**
* Checks if a user is logged in.
*
* @return boolean
*/
public function getLoggedIn(){
return $this->getContainer('auth')->isLoggedIn();
}
/**
* @param string $username
* @param string $password
* return boolean
*/
public function postLogin($username, $password){
return $this->getContainer('auth')->doLogin($username, $password);
}
/**
* @param string $server
* @url stats/([0-9]+)
* @url stats
* @return string
*/
public function getStats($server = '1'){
return $this->getServerStats($server);
}
}
use RestService\Server;
Server::create('/admin', new MyRestApi\Admin) //base entry points `/admin`
->setDebugMode(true) //prints the debug trace, line number and file if a exception has been thrown.
->addGetRoute('login', 'doLogin') // => /admin/login
->addGetRoute('logout', 'doLogout') // => /admin/logout
->addGetRoute('page', 'getPages')
->addPutRoute('page', 'addPage')
->addGetRoute('page/([0-9]+)', 'getPage')
->addDeleteRoute('page/([0-9]+)', 'deletePage')
->addPostRoute('page/([0-9]+)', 'updatePage')
->addGetRoute('foo/bar/too', 'doFooBar')
->addSubController('tools', \RestApi\Tools) //adds a new sub entry point 'tools' => admin/tools
->addDeleteRoute('cache', 'clearCache')
->addGetRoute('rebuild-index', 'rebuildIndex')
->done()
->run();
namespace MyRestApi;
class Admin {
public function login($username, $password){
if (!$this->validLogin($username, $password))
throw new InvalidLoginException('Login is invalid or no access.');
return $this->getToken();
}
public function logout(){
if (!$this->hasSession()){
throw new NoCurrentSessionException('There is no current session.');
}
return $this->killSession();
}
public function getPage($id){
//...
}
}
namespace RestAPI;
class Tools {
/**
* Clears the cache of the app.
*
* @param boolean $withIndex If true, it clears the search index too.
* @return boolean True if the cache has been cleared.
*/
public function clearCache($withIndex = false){
return true;
}
}
json
{
"arcj/php-rest-service": "*"
}
}
// edit virtualhost /etc/nginx/conf.d/name_virtualhost_file
server {
.. something params ...
location / {
}
}
// and add line to /etc/nginx/fastcgi_params
fastcgi_param PATH_INFO $fastcgi_script_name;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.