php-task1/index.php

67 lines
1.3 KiB
PHP
Raw Normal View History

2024-05-15 12:00:13 +03:00
<?php
ini_set("display_errors", 1);
error_reporting(-1);
const ROOT_PATH = __DIR__;
const BASE_URL = "http://php-task1";
include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/bootstrap/db.php';
use Phroute\Phroute\RouteCollector;
use Phroute\Phroute\Dispatcher;
use Illuminate\Database\Eloquent;
use Illuminate\Database\Eloquent\Model;
use ps\app\Models\Posts;
function processInput($uri){
$uri = urldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
return $uri;
}
function processOutput($response){
echo json_encode($response);
}
// function getPDOInstance(){
// return new PDO('mysql:host=localhost;dbname=booksapi;charset=utf8', 'root', '');
// }
$router = new RouteCollector();
$router->get('/', function(){
return 'Like at Home!';
});
$router->get('posts', function(){
echo Posts::all();
});
$router->get('login', [\ps\app\Controllers\LoginController::class, 'actionGetLoginForm']);
$dispatcher = new Dispatcher($router->getData());
try {
$response = $dispatcher->dispatch($_SERVER['REQUEST_METHOD'], processInput($_SERVER['REQUEST_URI']));
} catch (Phroute\Exception\HttpRouteNotFoundException $e) {
var_dump($e);
die();
} catch (Phroute\Exception\HttpMethodNotAllowedException $e) {
var_dump($e);
die();
}
processOutput($response);