Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once __DIR__ . '/vendor/autoload.php';
- $dsn = 'mysql:dbname=countries;host=127.0.0.1';
- $user = 'karlis';
- $password = '@Tests12345';
- try {
- $dbh = new PDO($dsn, $user, $password);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e->getMessage();
- }
- $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $router) {
- $router->addRoute('GET', '/', 'CountriesController@index');
- $router->addRoute('POST', '/', 'CountriesController@add');
- $router->addRoute('GET', '/countries/{id:\d+}', 'CountriesController@distinctCountry');
- });
- $httpMethod = $_SERVER['REQUEST_METHOD'];
- $uri = $_SERVER['REQUEST_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:
- break;
- case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
- $allowedMethods = $routeInfo[1];
- break;
- case FastRoute\Dispatcher::FOUND:
- $handler = $routeInfo[1];
- $params = $routeInfo[2];
- [$controller, $method] = explode('@', $handler);
- $controllerPath = '\App\Controllers\\' . $controller;
- echo (new $controllerPath)->{$method}($params);
- break;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement