Advertisement
zukars3

Untitled

Apr 19th, 2020
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2.  
  3. require_once __DIR__ . '/vendor/autoload.php';
  4.  
  5. $dsn = 'mysql:dbname=countries;host=127.0.0.1';
  6. $user = 'karlis';
  7. $password = '@Tests12345';
  8.  
  9. try {
  10.     $dbh = new PDO($dsn, $user, $password);
  11. } catch (PDOException $e) {
  12.     echo 'Connection failed: ' . $e->getMessage();
  13. }
  14.  
  15. $dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $router) {
  16.     $router->addRoute('GET', '/', 'CountriesController@index');
  17.     $router->addRoute('POST', '/', 'CountriesController@add');
  18.     $router->addRoute('GET', '/countries/{id:\d+}', 'CountriesController@distinctCountry');
  19. });
  20.  
  21. $httpMethod = $_SERVER['REQUEST_METHOD'];
  22. $uri = $_SERVER['REQUEST_URI'];
  23.  
  24. if (false !== $pos = strpos($uri, '?')) {
  25.     $uri = substr($uri, 0, $pos);
  26. }
  27. $uri = rawurldecode($uri);
  28.  
  29. $routeInfo = $dispatcher->dispatch($httpMethod, $uri);
  30. switch ($routeInfo[0]) {
  31.     case FastRoute\Dispatcher::NOT_FOUND:
  32.         break;
  33.     case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
  34.         $allowedMethods = $routeInfo[1];
  35.         break;
  36.     case FastRoute\Dispatcher::FOUND:
  37.         $handler = $routeInfo[1];
  38.         $params = $routeInfo[2];
  39.  
  40.         [$controller, $method] = explode('@', $handler);
  41.  
  42.         $controllerPath = '\App\Controllers\\' . $controller;
  43.         echo (new $controllerPath)->{$method}($params);
  44.  
  45.         break;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement