PHP Classes

File: src/Route/routes.php

Recommend this page to a friend!
  Classes of Pierre-Henry Soria   PHP RESTful API Example   src/Route/routes.php   Download  
File: src/Route/routes.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP RESTful API Example
Example implementation of a REST API
Author: By
Last change:
Date: 7 months ago
Size: 1,244 bytes
 

Contents

Class file image Download
<?php
/**
 * @author Pierre-Henry Soria <hi@ph7.me>
 * @website https://ph7.me
 * @license MIT License
 */

namespace PH7\ApiSimpleMenu\Route;

use
PH7\ApiSimpleMenu\Route\Exception\NotFoundException;
use
PH7\ApiSimpleMenu\Service\Exception\CredentialsInvalidException;
use
PH7\ApiSimpleMenu\Validation\Exception\InvalidValidationException;
use
PH7\JustHttp\StatusCode;
use
PH7\PhpHttpResponseHeader\Http as HttpResponse;

$resource = $_REQUEST['resource'] ?? null;

try {
    return
match ($resource) {
       
'user' => require_once 'user.routes.php',
       
'item' => require_once 'food-item.routes.php',
        default => require_once
'not-found.routes.php',
    };
} catch (
CredentialsInvalidException $e) {
   
response([
       
'errors' => [
           
'message' => $e->getMessage()
        ]
    ]);
} catch (
InvalidValidationException $e) {
   
// Send 400 http status code
   
HttpResponse::setHeadersByCode(StatusCode::BAD_REQUEST);

   
response([
       
'errors' => [
           
'message' => $e->getMessage(),
           
'code' => $e->getCode()
        ]
    ]);
} catch (
NotFoundException $e) {
   
// FYI, not-found.Route already sends a 404 Not Found HTTP code
   
return require_once 'not-found.routes.php';
}