PHP Classes

File: api/config.php

Recommend this page to a friend!
  Classes of Okanlawon Anuoluwapo   PHP MongoDB CRUD Example of API   api/config.php   Download  
File: api/config.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP MongoDB CRUD Example of API
API that provides CRUD operations with MongoDB
Author: By
Last change:
Date: 8 months ago
Size: 1,270 bytes
 

Contents

Class file image Download
<?php
header
('Access-Control-Allow-Origin: *');
header('Content-Type:application/json');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
header('Access-Control-Allow-Credentials: true');
//
ini_set("display_errors", 1);

require
__DIR__ . "/vendor/autoload.php";

$dotenv = Dotenv\Dotenv::createUnsafeImmutable(__DIR__);
$dotenv->load();

// Set the new timezone
date_default_timezone_set('Africa/Lagos');

define('MONGO_URI', getenv('MONGO_URI'));

/** Sets up webpdo db vars */
global $db;

if ( isset(
$db) ) return;

$db = (new MongoDB\Client)->mdb;

function
createCollectionIfNotExist($db) {
   
$collections = ["users", "products"];
   
//
   
$colls = [];
    foreach (
$db->listCollectionNames() as $collectionName) $colls[] = $collectionName;
   
//
   
foreach ($collections as $coll) {
        if ( !
in_array($coll, $colls) ) {
           
$db->createCollection($coll);
        }
    }
}
// createCollectionIfNotExist($db);

spl_autoload_register(function($class){
   
$filepath = str_replace(
       
'\\', // Replace all namespace separators...
       
'/', // ...with their file system equivalents
       
__DIR__ . "/classes/{$class}.php"
   
);

    if (
is_file(strtolower($filepath)))
        require_once
strtolower($filepath);
});