PHP Classes

File: app/Router/UsuarioRouter.php

Recommend this page to a friend!
  Packages of Rodrigo Faustino   PHP CRUD REST API   app/Router/UsuarioRouter.php   Download  
File: app/Router/UsuarioRouter.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP CRUD REST API
Implement an example REST API using MVC
Author: By
Last change:
Date: 2 years ago
Size: 1,231 bytes
 

 

Contents

Class file image Download
<?php
require "../../vendor/autoload.php";

use App\Controller\UsuarioController;
use App\Http\HttpHeader;
use App\Repository\UsuarioRepository;

HttpHeader::setDefaultHeaders();

$repository = new UsuarioRepository(); 
$controller = new UsuarioController($repository);

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}
$action = 'default'; 
if (isset($_GET['action'])) {
    $action = $_GET['action']; 
}
$data = json_decode(file_get_contents("php://input"));

switch ($_SERVER['REQUEST_METHOD']) {
    case 'POST':
        switch ($action) {
            case 'login':
                $controller->login($data);
                break;
            default:
                $controller->create($data);
                break;
        }
        break;
    case 'GET':
        $id = isset($_GET['id']) ? $_GET['id'] : null;
        $controller->read($id);
        break;
    case 'PUT':
        $controller->update($data);
        break;
    case 'DELETE':
        $id = isset($_GET['id']) ? $_GET['id'] : null;
        if ($id === null) {
            HttpHeader::sendNotAllowedMethod();
        }
        $controller->delete($id);
        break;
    default:
        HttpHeader::sendNotAllowedMethod();
        break;
}