PHP Classes

File: api/index.php

Recommend this page to a friend!
  Packages of Faris AL-Otabi   Simple PHP API Framework   api/index.php   Download  
File: api/index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple PHP API Framework
Create new APIs using controller classes
Author: By
Last change:
Date: 2 years ago
Size: 1,479 bytes
 

 

Contents

Class file image Download
<?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;
}