https://github.com/lamansky/api
Lets you create REST APIs using PHP classes to represent API endpoints.
https://github.com/lamansky/api
api php rest-api
Last synced: 2 months ago
JSON representation
Lets you create REST APIs using PHP classes to represent API endpoints.
- Host: GitHub
- URL: https://github.com/lamansky/api
- Owner: lamansky
- License: mit
- Created: 2017-10-09T09:44:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-06-06T22:16:18.000Z (about 5 years ago)
- Last Synced: 2025-01-28T16:45:22.454Z (over 1 year ago)
- Topics: api, php, rest-api
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# Lamansky/Api
Lets you create REST APIs using PHP classes to represent API endpoints.
## Installation
With [Composer](http://getcomposer.org) installed on your computer and initialized for your project, run this command in your project’s root directory:
```bash
composer require lamansky/api
```
Requires PHP 7.4 or above.
## Usage Tutorial
### Introduction
An endpoint is a URL (or URL pattern) that can receive REST commands. Every endpoint in your API will be represented by a PHP class. This PHP class implements an `Endpoint` interface appropriate to the types of REST commands it can accept.
| | GET | POST | PUT | DELETE |
|---------------------:|:---:|:----:|:---:|:------:|
| `CollectionEndpoint` | ✔ | ✔ | | |
| `ItemEndpoint` | ✔ | | ✔ | ✔ |
| `ReadOnlyEndpoint` | ✔ | | | ||
Each REST command is implemented as a public method of the endpoint controller:
```php
registerEndpoint(new HelloWorldEndpoint());
$api->getResponder()->sendResponseAndDie();
```
You’ll also need to make sure that the server is routing all requests to the above file. Assuming this file is named `index.php` and you’re running Apache, you would create an `.htaccess` file like this:
```
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
```
You now have a working API! If your site is running on `localhost`, then the API will output `Hello world!` when you send a GET command to `http://localhost/api/hello-world/`.
### URL Variables
So far we’ve seen how to create an endpoint with a static URL. But what if we need to manipulate an item with a given ID?
```php
$blog_post->id, 'title' => $blog_post->title, 'content' => $blog_post->content];
}
}
class BlogPostItemEndpoint implements ItemEndpoint {
public function getRoutePattern() : string {
return '/post/[i:id]/';
}
public function get(int $id=null) : Responder {
// TODO: Use the ID to get the BlogPost object from your database
return (new BlogPostJsonView())->single($blog_post);
}
public function put(int $id=null) : Responder { return new Responder(Responder::NOT_IMPLEMENTED); }
public function delete(int $id=null) : Responder { return new Responder(Responder::NOT_IMPLEMENTED); }
}
class BlogPostCollectionEndpoint implements CollectionEndpoint {
public function getRoutePattern() : string {
return '/post/';
}
public function get() : Responder {
// TODO: Get all the BlogPost objects in an array
return (new BlogPostJsonView())->multiple($blog_posts);
}
public function post(int $id=null) : Responder { return new Responder(Responder::NOT_IMPLEMENTED); }
}
```
### Complete Example
```php
single($test);
}
public function put(int $id=null) : Responder {
return new Responder(Responder::NOT_IMPLEMENTED);
}
public function delete(int $id=null) : Responder {
return new Responder(Responder::FORBIDDEN);
}
}
class Test {
public $id;
public function __construct(int $id) {
$this->id = $id;
}
}
class TestJsonView extends JsonView {
public function render($test) : array {
return ['id' => $test->id];
}
}
$api = new Api('/api');
$api->registerEndpoint(new TestItemEndpoint());
$api->getResponder()->sendResponseAndDie();
```
A GET request to `http://localhost/api/test/1/` will produce:
```json
{
"id": 1
}
```
## Version Migration Guide
Here are backward-incompatible changes you need to know about.
### 1.x ⇒ 2.x
* The minimum supported PHP version is now 7.4 (instead of 7.1).
* The [darsyn/ip](https://github.com/darsyn/ip) dependency has been updated to version 4.x, which may introduce some backward-incompatible changes for those who relied on the public API of the `Darsyn\IP\IP` object formerly returned by the `Client::instance()->getIp()` method. This method now returns a `Lamansky\Api\IpAddress` object which wraps around the `darsyn/ip` library and will serve as a buffer against further backward-incompatible changes from that dependency. If you were previously using the `Darsyn\IP\Doctrine\IpType` Doctrine2 type, consider replacing it with the compatibility wrapper `Lamansky\Api\Doctrine\IpAddressType`.