raframework/raframework

There is no license information available for the latest version (v0.0.13) of this package.

A RESTful API framework

v0.0.13 2018-04-04 06:01 UTC

This package is not auto-updated.

Last update: 2024-04-18 07:13:35 UTC


README

A RESTful API framework for the PHP language.

Getting Started

  1. Create a raframework project name raproj

    $ mkdir raproj && cd raproj
  2. Create a composer.json file with the following contents:

    {
      "require": {
        "raframework/raframework": "0.0.10"
      },
      "autoload": {
        "psr-4": {
          "App\\": "App/"
        }
      }
    }
  3. Install the raframework by Composer

    $ composer install
  4. Make the resource classes directory

    $ mkdir -p App/Resource
  5. Create an App/Resource/Users.php file with the following contents:

    <?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);
        }
    }
  6. Create an index.php file with the following contents:

    <?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();
  7. You may quickly test this using the built-in PHP server:

    $ php -S localhost:8800

    Going to http://localhost:8800/users will now display "List users...".