PHP Classes

Simple PHP API Framework: Create new APIs using controller classes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-27 (8 months ago) RSS 2.0 feedNot enough user ratingsTotal: 73 All time: 10,228 This week: 47Up
Version License PHP version Categories
api_framework 1.0.0Custom (specified...5Web services, PHP 7
Description 

Author

This package can be used to create new APIs using controller classes.

It provides a base controller class to implement the actions of API calls.

API developers should create base controller sub-classes to implement controller classes that implement the API endpoint actions.

Picture of Faris AL-Otabi
  Performance   Level  
Name: Faris AL-Otabi <contact>
Classes: 16 packages by
Country: Saudi Arabia Saudi Arabia
Age: 27
All time rank: 275610 in Saudi Arabia Saudi Arabia
Week rank: 6 Up1 in Saudi Arabia Saudi Arabia Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?php

declare(strict_types=1);

include_once
__DIR__ . "/bootstrap.php";

ini_set('display_errors', 1);
error_reporting(E_ALL);

use
MY_Framework\TaskController;
use
MY_Framework\Auth;
use
MY_Framework\Database;
use
MY_Framework\TaskGateway;
use
MY_Framework\UserGateway;
use
MY_Framework\UserController;
use
MY_Framework\RefreshTokenGateway;

$database = new Database(
   
$_ENV['DB_HOST'],
   
$_ENV['DB_NAME'],
   
$_ENV['DB_USER'],
   
$_ENV['DB_PASS']
);

$user_gateway = new UserGateway($database);

$rtg = new RefreshTokenGateway($database, $secret_key);

$user = new UserController($user_gateway, $codec, $rtg);

$auth = new Auth($user_gateway, $codec);

$auth_needed_routes = ["tasks"];

if (
in_array($route, $auth_needed_routes)) {
   
$auth->authenticate($_ENV['AUTHENTICATE_BY']);

   
$user_id = $auth->getUserId();
}

/** API Router */
switch ($route) {
    case
'tasks':
       
$task_gatway = new TaskGateway($database);

       
$task_controller = new TaskController($task_gatway, $user_id);

       
$task_controller->processRequest($method, $id);

        break;

    case
'register':
       
$user->register($method);

        break;

    case
'login':
       
$user->login($method);
        break;

    case
'logout':
       
$user->logout($method);
        break;

    case
'refresh':
       
$user->refresh($method);
        break;

    default:
       
http_response_code(404);
        echo
json_encode(["message" => "Page not found"]);
        break;
}


Details

API_Framework

A PHP Framework for creating APIs

<a href="http://jwt.io"> <img src="http://jwt.io/img/badge-compatible.svg" /> </a>

PHP Version

This project is created using PHP 8.0+

Coding Standard is PSR-12

Installation

$ composer update
$ composer dump-autoload

Import api.sql to your phpmyadmin

Generate 256-bit key from here allkeysgenerator

Update .env file

The system has 2 authentication methods

  1. JWT Token [token]
  2. API Key [key]

After that just create an account using /register endpoint and POST method

$ httpie post http://localhost/api/register name="Root" username="admin" password="admin"

or

Use the example to create an account

You can reuse the example for any project you want

Enjoy

Example Project

TaskController.php

TaskGateway.php

Are an example project to show how the system work

Also you can reuse the code in it and change it to your specifications

License

MIT

Copyright

2022 - fariscode


  Files folder image Files (23)  
File Role Description
Files folder imageapi (3 files)
Files folder imageexample (2 files)
Files folder imagesrc (1 directory)
Accessible without login Plain text file api.sql Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file delete-expired-refresh-tokens.php Example Example script
Accessible without login Plain text file envfile Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (23)  /  api  
File Role Description
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Accessible without login Plain text file bootstrap.php Example Example script
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files (23)  /  example  
File Role Description
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login Plain text file register.php Example Example script

  Files folder image Files (23)  /  src  
File Role Description
Files folder imageMY_Framework (10 files, 1 directory)

  Files folder image Files (23)  /  src  /  MY_Framework  
File Role Description
Files folder imageExceptions (2 files)
  Plain text file Auth.php Class Class source
  Plain text file BaseController.php Class Class source
  Plain text file Database.php Class Class source
  Plain text file ErrorHandler.php Class Class source
  Plain text file JWTCodec.php Class Class source
  Plain text file RefreshTokenGateway.php Class Class source
  Plain text file TaskController.php Class Class source
  Plain text file TaskGateway.php Class Class source
  Plain text file UserController.php Class Class source
  Plain text file UserGateway.php Class Class source

  Files folder image Files (23)  /  src  /  MY_Framework  /  Exceptions  
File Role Description
  Plain text file InvalidSignatureException.php Class Class source
  Plain text file TokenExpiredException.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:73
This week:0
All time:10,228
This week:47Up