https://github.com/filippofinke/php-rest
📦 Simple php rest api framework
https://github.com/filippofinke/php-rest
api framework php rest router
Last synced: 17 days ago
JSON representation
📦 Simple php rest api framework
- Host: GitHub
- URL: https://github.com/filippofinke/php-rest
- Owner: filippofinke
- Created: 2019-11-22T21:56:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-06T18:03:36.000Z (about 6 years ago)
- Last Synced: 2025-06-18T07:45:34.016Z (about 1 year ago)
- Topics: api, framework, php, rest, router
- Language: PHP
- Homepage:
- Size: 51.8 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-rest
---
## Description
A very simple, basic and powerful rest api framework.
## Install
```
composer require filippofinke/php-rest
```
```php
start();
```
## Add a route
```php
get('/', function($req, $res) {
// Route / logic for get
});
// Add a get route with regex
$router->get('/([a-zA-Z]*)', function($req, $res) {
// Get regex match
$name = $req->getAttribute('args')[1];
// Route logic
});
// Add a post route
$router->post('/', function($req, $res) {
// Route / logic for post
});
// Add multiple methods route
$router->map(['get', 'post'], '/', function($req, $res) {
// Route / logic for get and post
});
```
## Request
```php
get('/', function($req, $res) {
// See all methods in src/Request.php
// Get request ip address
$address = $req->getRemoteAddress();
// Get request parameters
$params = $req->getParams();
$response = array();
$response[] = $address;
$response[] = $params;
// Return response
return $res->withJson($response);
});
```
## Response
```php
get('/', function($req, $res) {
// See all methods in src/Response.php
// Add header
$req->withHeader('Test-Header', 'Value');
// Response with text
$req->withText('This is the / route');
// Append text to the content
$req->append(PHP_EOL.'a new line');
// Set status, default 200
$req->setStatus(200);
// You can also call all of these methods as a chain
// $req->withHeader('Test-Header', 'Value')->withText('This is the / route')->setStatus(200);
// Return response
return $res;
});
```
## Redirect
```php
get('/', function($req, $res) {
// Redirect to route and with status code
return $res->redirect('/login')->withStatus(301);
});
```
## Before and after middlewares
```php
withHeader("Before-Header", time());
};
$after = function($req, $res) {
$req->withHeader("After-Header", time());
};
// Add a get route
$router->get('/', function($req, $res) {
// Route / logic for get
})
// Add before middleware
->before($before);
// Add after middleware
->after($after)
```