api.ampere/public/index.php

40 lines
1.1 KiB
PHP

<?php
require "../bootstrap.php";
use Src\Controller\CompteurController;
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );
// all of our endpoints start with /???
// everything else results in a 404 Not Found
$endpoints = array(
'compteur',
'releve'
);
if ( ! in_array($uri[1], $endpoints) ) {
header("HTTP/1.1 404 Not Found");
exit();
}
// the user id is, of course, optional and must be a number:
$id = null;
if (isset($uri[2])) {
$id = (int) $uri[2];
}
$requestMethod = $_SERVER["REQUEST_METHOD"];
// pass the request method and user ID to the CompteurController and process the HTTP request:
$qsd = CompteurController
$controller = new $qsd($dbConnection, $requestMethod, $id);
$controller->processRequest();