Corna-Rest, working with minimum configuration for create basically rest apis. Even, if you are not use database, you not need even configuration. Install, create routes and run
- Lightweight, 61.9kb
- Add Middlewares to routes
- Create custom libraries
- Contains database helper - DBHelper For PHP
- You can use multiple files for your routes
- You can create routes for different methods [POST, GET]
// In app/routes/main.php
// Add '' for root url
$routes->get('', function () {
echo "Hello World!";
});
First, create a php file in app/routes
folder. After, you can add route like examples.
Basically Usage
$routes->get('/users', function () {
// CONTENT
});
With Parameters
$routes->get('/user/{id}', function ($id) {
// CONTENT
});
Basically Usage
$routes->post('/new_user', function () {
// CONTENT
});
With Parameters
$routes->post('/new_user', function () {
// You can use default $_POST varaible for post parameters.
echo $_POST['user_name'];
});
You can use $response
variable to create response. If you need $response
variable, you need add use
to your function.
$routes->get('/users', function () use ($response) {
$users = array();
$response->response($users);
});
$routes->get('/users', function () use ($response) {
$users = array();
$response->statusCode(200)->response($users);
});
First, create a php class in app/middleware
folder. The skeleton of this class should be,
// MyMiddleware.php
namespace App\Middleware;
use App\Core\Middleware;
class MyMiddleware extends Middleware {
function next($params) {
return true;
}
}
If you don't add return true
or add return false
, the request cannot be completed.
If you route contain parameters, you can use this parameters in $params
variable.
And usage,
$routes->middleware('mymiddleware')->get('/users', function () use ($response) {
$users = array();
$response->statusCode(200)->response($users);
});
First, create a php class in app/library
folder. The skeleton of this class should be,
// MyLibrary.php
namespace App\Library;
class MyLibrary {
}
And usage,
use App\Library\MyLibrary;
$routes->get('/users', function () use ($response) {
$library = new MyLibrary();
});
You can use $db
variable for Database Helper. If you need $db
variable, you need add use
to your function like $response
.
Usage
$routes->get('/users', function () use ($response, $db) {
$db->connect();
$users = $db->get('users');
$response->response($users);
$db->disconnect();
});
If you need a detailed document for database helper, you can visit DBHelper For PHP repo.