This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
63 lines (39 loc) · 1.65 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require_once __DIR__ . '/vendor/autoload.php';
use App\Core\Database;
use Medoo\Medoo;
$databaseConfig = require_once __DIR__ . '/config/configdata.php';
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $router) {
$router->addRoute('GET', '/', 'CountriesController@index');
$router->addRoute('GET', '/countries/{id:\d+}', 'CountriesController@show');
$router->addRoute('POST', '/country/add', 'CountriesController@addCountry');
$router->addRoute('POST', '/countries/{id:\d+}', 'CountriesController@addCity');
$router->addRoute('POST','/delete/countries/{id:\d+}','CountriesController@deleteCountry');
$router->addRoute('POST','/delete/cities/{id:\d+}','CountriesController@deleteCity');
});
// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
// Strip query string (?foo=bar) and decode URI
if (false !== $pos = strpos($uri, '?')) {
$uri = substr($uri, 0, $pos);
}
$uri = rawurldecode($uri);
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);
switch ($routeInfo[0]) {
case FastRoute\Dispatcher::NOT_FOUND:
// ... 404 Not Found
break;
case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
$allowedMethods = $routeInfo[1];
// ... 405 Method Not Allowed
break;
case FastRoute\Dispatcher::FOUND:
$handler = $routeInfo[1];
$params = $routeInfo[2];
// ... call $handler with $vars
[$controller, $method] = explode('@', $handler);
$controllerPath = '\App\Controllers\\' . $controller;
echo (new $controllerPath)->{$method}($params);
break;
}