Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rozbehsharahi/fhp-rest-api
Super quick setup of a PHP-REST-API without the use of MySQL, Apache, ...
https://github.com/rozbehsharahi/fhp-rest-api
Last synced: about 1 month ago
JSON representation
Super quick setup of a PHP-REST-API without the use of MySQL, Apache, ...
- Host: GitHub
- URL: https://github.com/rozbehsharahi/fhp-rest-api
- Owner: RozbehSharahi
- Created: 2017-01-22T22:22:15.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-02-20T23:31:44.000Z (over 7 years ago)
- Last Synced: 2024-09-30T05:45:06.372Z (about 2 months ago)
- Language: PHP
- Size: 63.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# FHP REST API [![Build Status](https://travis-ci.org/RozbehSharahi/fhp-rest-api.svg?branch=v2.0.0)](https://travis-ci.org/RozbehSharahi/fhp-rest-api) (Master [![Build Status](https://travis-ci.org/RozbehSharahi/fhp-rest-api.svg?branch=master)](https://travis-ci.org/RozbehSharahi/fhp-rest-api))
## Description
FHP REST API provides an easy way to setup a __JSON FILE based
REST API__.There are no dependencies to MySQL or other databases. All files will be
stored as json files by default. Still you may also integrate your own
way of saving data, by integrating your own __controllers__, __entites__ and
__normalizers__.I'm trying really hard to make this very flexible and at the same easy
to understand, i hope you enjoy working with this.## Installation
```shell
$ composer require rozbehsharahi/fhp-rest-api
```## How to
The following __index.php__ is enough to provide PUT, GET, POST, OPTION, DELETE
routes for your application on the `/posts` route:```php
activateEntity('post', FlexEntityController::class) // <-- use singular entity name
->run();
```That's it!
### How to use entity classes
(Still experimental)
This way of using FHP REST API is still very experimental, so please
used this with caution. For instance you have to define fhp property
type annotations, though they don't have any impact on the way a property
is saved yet.```php
activateEntity(My\Example\Page::class)
->run();
```Your entity class should look like this:
```php
id; }public function setId($id) { $this->id = $id; return $this; }
public function getUrl() { return $this->url; }
public function setUrl($url) { $this->url = $url; return $this; }
}
```At the moment it does not make any difference which property type you will use. But
it is a good preparation for next releases. You will still have to annotate with
one of the following annotations:* `@Fhp\Rest\PropertyType\StringType`
* `@Fhp\Rest\PropertyType\BooleanType`
* `@Fhp\Rest\PropertyType\IntegerType`### Defining custom entity-controllers
You may also define your own controllers to handle model-requests. Just pass
your controller's class name as the third parameter to `$api->createModel`.Please extend `Fhp\Rest\Controller` and
feel free to override the basic actions:* `public function indexAction($request, $response, $args) {}`
* `public function showAction($request, $response, $args) {}`
* `public function updateAction($request, $response, $args) {}`
* `public function deleteAction($request, $response, $args) {}`
* `public function createAction($request, $response, $args) {}`by calling `$api->activateEntity` like following:
```php
$api->activateEntity(
\My\Entity\Post::class,
\My\Own\PostController::class
);
```## Super fast start without Apache
No apache, No MySQL, just make sure PHP is installed on your machine.
Since FHP REST API is super independent you may start your server
on almost any machine that supports PHP7+ (in future also PHP5.6+). Not even Apache is
needed to get it started. Just change your directory to the projects root and go on with:```shell
$ php -S localhost:8000
```## Start with Apache
In case you want to start FHP REST API on an apache server, please add
the following `.htaccess` file to your project's root where
`index.php` is located.```htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
```## Security issues / CORS
FHP REST API has a set of default-headers that are defined in `Fhp\Rest\Api`.
These headers also contain `'Access-Control-Allow-Origin' => '*'` which is
a security issue depending on the project.Please make sure to configure your own header-settings
when going to production.You may do this by following:
```php
$api->setHeaders([
'Content-Type' => 'application/json',
'Access-Control-Allow-Origin' => 'http://my-specific-doain.com'
]);
```## Todos and issues
* PropertyTypes
* Refactoring