Wallogit.com
2017 © Pedro Peláez
A RESTful API framework
A RESTful API framework for the PHP language., (*1)
Create a raframework project name raproj, (*2)
$ mkdir raproj && cd raproj
Create a composer.json file with the following contents:, (*3)
{
"require": {
"raframework/raframework": "0.0.10"
},
"autoload": {
"psr-4": {
"App\\": "App/"
}
}
}
Install the raframework by Composer, (*4)
$ composer install
Make the resource classes directory, (*5)
$ mkdir -p App/Resource
Create an App/Resource/Users.php file with the following contents:, (*6)
<?php
namespace App\Resource;
use Ra\Http\Request;
use Ra\Http\Response;
// Define resource's class.
class Users
{
// Define the resource's action.
public function lis(Request $request, Response $response)
{
$data = 'List users...';
$response->withStatus(200)->write($data);
}
}
Create an index.php file with the following contents:, (*7)
<?php
require 'vendor/autoload.php';
// Define the routes.
$uriPatterns = [
'/users' => ['GET'], // uri pattern => supported methods
];
// Create a raframework app with the routes given.
$app = new Ra\App($uriPatterns);
// Match the route, and set the resource's action correctly.
$app->matchUriPattern()
// Call the resource's action.
// You should call matchUriPattern() before this.
->callResourceAction()
// Send the response to the client.
->respond();
You may quickly test this using the built-in PHP server:, (*8)
$ php -S localhost:8800
Going to http://localhost:8800/users will now display "List users..."., (*9)