Download the PHP package xp-forge/rest-api without Composer
On this page you can find all versions of the php package xp-forge/rest-api. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xp-forge/rest-api
More information about xp-forge/rest-api
Files in xp-forge/rest-api
Package rest-api
Short Description Rest API
License BSD-3-Clause
Homepage http://xp-framework.net/
Informations about the package rest-api
Rest APIs
Annotation-based REST APIs
Example
Wire it together in a web application:
Run it using:
Then call curl -i localhost:8080/users/1549
.
Parameter sources
Method parameters are automatically extracted from URI segments if their name matches the path segment in the curly braces. For requests without bodies (GET, HEAD, DELETE, OPTIONS), the value is extracted from request parameters. For requests with bodies (POST, PUT and PATCH), the body is deserialized and passed.
To supply the source explicitely, you can use parameter attributes:
#[Param]
will fetch the parameter from the request parameter named "max".#[Param('maximum')]
will fetch the parameter from the request parameter named "maximum".#[Value]
will use a request value (which was previously passed e.g. inside a filter viapass()
) for the parameter#[Header('Content-Type')]
will use the Content-Type header as value for the parameter#[Entity]
will deserialize the request body and pass its value to the parameter#[Body]
will pass the request body as a string#[Stream]
will pass anio.streams.InputStream
instance to stream the request body to the parameter#[Request]
will pass the completeweb.Request
object
Parameter conversions
Parameters can be converted from their input. This library comes with a built-in conversion named SeparatedBy:
The orgunits parameter can now be supplied in the URL as follows: https://example.com/api/trainings/completed?orgunits=A,B
and the resulting value inside $orgunits will be ["A", "B"]
. User-defined conversions can be supplied by implementing the web.rest.Conversion
interface.
Matrix parameters
This library supports parameters inside path segments, e.g. https://example.com/api/trainings/status=COMPLETED;orgunits=A,B/authors
:
The resulting value inside $filter will be ["status" => "COMPLETED", "orgunits" => ["A", "B"]]
.
Return types
Methods can return anything, which is then serialized and written to the response with a "200 OK" status. If you want greater control over the response, you can use the web.rest.Response
class. It provides a fluent DSL for handling various scenarios.
Example:
Creation:
Response::ok()
- 200 OKResponse::created([string $location])
- 201 Created, optionally with a Location headerResponse::noContent()
- 204 No contentResponse::see(string $location)
- 302 Found and a Location headerResponse::notModified()
- 304 Not modifiedResponse::notFound([string $message])
- 404 Not found and an optional message, which is serializedResponse::notAcceptable([string $message])
- 406 Not acceptable and an optional message, which is serializedResponse::error(int $code[, string $message])
- An error and an optional message, which is serializedResponse::status(int $code)
- Any other status code
Headers:
$response->type(string $mime)
will set the Content-Type header$response->header(string $name, string $value)
will set a header with a given name and value
Body:
$response->entity(var $value)
will sent a value, serializing it$response->stream(io.streams.InputStream $in[, int $size])
will stream a response$response->body(string $bytes)
will write the given raw bytes to the response
Asynchronous invocation
The following code will run the upload function asynchronously, continuing to serve requests while file contents are being transmitted.
See also
https://github.com/thekid/shorturl - URL Shortener service
All versions of rest-api with dependencies
xp-framework/reflection Version ^3.0 | ^2.0
xp-forge/web Version ^4.0 | ^3.0 | ^2.0 | ^1.0
xp-forge/marshalling Version ^2.0 | ^1.0
xp-forge/json Version ^5.0 | ^4.0 | ^3.1
php Version >=7.0.0