PHP REST Service is a simple and fast PHP class for RESTful JSON APIs.
- Easy to use syntax
- Regular Expression support
- Error handling through PHP Exceptions
- JSON, XML, and plain text responses
- Automatic OpenAPI specification generation
- Parameter validation through PHP function signature
- Can return a summary of all routes or one route through
OPTIONS
method based on PHPDoc (ifOPTIONS
is not overridden) - Support of
GET
,POST
,PUT
,DELETE
,PATCH
,HEAD
andOPTIONS
- Suppress the HTTP status code with ?_suppress_status_code=1 (for clients that have troubles with that)
- Supports custom error handling, logging, access control and response formatting functions.
- Supports ?_method=
httpMethod
as addition to the actual HTTP method. - With auto-generation through PHP's
reflection
- PHP 7.4+ (Tested on PHP 7.4 - 8.2)
php composer require cdgco/php-rest-service
use RestService\Server;
Server::create('/')
->addGetRoute('test/(\D+)', function($param){
return 'Yay!' . $param; // $param pulled from URL capture group
})
->addPostRoute('foo', function($field1) {
return 'Hello ' . $field1; // same as "return 'Hello ' . $_POST('field1');"
})
->addGetRoute('use/this/name', function(){
return 'Hi there';
})
->run();
namespace MyRestApi;
use RestService\Server;
class Admin {
/*
* @url /test/(\d+)
*/
public function getTest($param) {
return 'Yay!' . $param; // $param pulled from URL capture group
}
public function postFoo($field1){
return 'Hello ' . $field1; // same as "return 'Hello ' . $_POST('field1');"
}
/*
* @url /use/this/name
*/
public function getNotThisName($field1){
return 'Hi there';
}
}
Server::create('/', 'myRestApi\Admin')
->collectRoutes()
->run();
Both methods will generate the following endpoints:
+ GET /test/:param
+ POST /foo
+ GET /use/this/name
Read the full documentation at https://cdgco.github.io/php-rest-service.
Licensed under the MIT License. See the LICENSE file for more details.