PHP code example of jacob-roth / phapi

1. Go to this page and download the library: Download jacob-roth/phapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jacob-roth / phapi example snippets


  $Routes = new \Phapi\Routes();
  $Routes->Add(new \Phapi\Route(
    "Default",
    "/api/v1/{controller}/{action}"
  ));

  $Startup = new \Phapi\Startup($Routes);
  $Startup->Run();
  

  class DefaultController extends \Phapi\Controller
  

    // This endpoint can be called like
    // PUT /Default/IdPut/2
    public function IdPut(int $id)
    {
      $this->HttpPut();
      return [
        "id" => $id
      ];
    }

    // This endpoint can be called like
    // GET /Default/2
    public function GET(int $id)
    {
      $this->HttpGet();
      return "something";
    }
    

    $this->HttpGet();
    $this->HttpPut();
    $this->HttpPost();
    $this->HttpDelete();
    

    $this->SetResponseCode(\Phapi\HttpCode::Created);
    

    public function __construct(
      string $Name,
      string $Path,
      ?array $HttpMethods = null,
      string $Namespace = "",
      ?string $DefaultController = null,
      ?string $DefaultAction = null
    )
    

          public function IdGet(int $id)
          

          public function IdGet(?int $id)
          

        [\Phapi\HttpMethod::Get, \Phapi\HttpMethod::Post]
        

        \Phapi\HttpMethod::All
        

        $Routes->Add(new \Phapi\Route(
          "Default",
          "/api/v1/{controller}/{action}",
          \Phapi\HttpMethod::All,
          "V1"
        ));
        

        $Routes->Add(new \Phapi\Route(
          "Calling Methods with Special URLs",
          "/{id}",
          null,
          "V2",
          "Special",
          "DoingSomethingSpecial"
        ));
        

    throw new \Phapi\ApiException(\Phapi\HttpCode::BadRequest, "Invalid Input Data");
    

    \Phapi\HttpMethod::Get
    \Phapi\HttpMethod::Head
    \Phapi\HttpMethod::Post
    \Phapi\HttpMethod::Put
    \Phapi\HttpMethod::Delete
    \Phapi\HttpMethod::Connect
    \Phapi\HttpMethod::Options
    \Phapi\HttpMethod::Trace
    \Phapi\HttpMethod::Patch
    \Phapi\HttpMethod::All
    

    \Phapi\HttpCode::Ok          // 200
    \Phapi\HttpCode::Created     // 201
    \Phapi\HttpCode::NotFound    // 404
    

    public function EchoId(RequestObject $req)
    {
      $this->HttpPost();
      return [
        "id" => $req->id
      ];
    }
    
apache
  <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule !index.php index.php
  </IfModule>