Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. <?php
  2. class Router
  3. {
  4.   protected $routes = [];
  5.   const NO_ROUTE = 1;
  6.  
  7.   public function addRoute(Route $route)
  8.   {
  9.     if (!in_array($route, $this->routes))
  10.     {
  11.       $this->routes[] = $route;
  12.     }
  13.   }
  14.  
  15.   public function getRoute($url)
  16.   {
  17.     foreach ($this->routes as $route)
  18.     {
  19.       if (($varsValues = $route->match($url)) !== false)
  20.       {
  21.         if ($route->hasVars())
  22.         {
  23.           $varsNames = $route->varsNames();
  24.           $listVars = [];
  25.  
  26.           foreach ($varsValues as $key => $match)
  27.           {
  28.             if ($key !== 0) $listVars[$varsNames[$key - 1]] = $match;
  29.           }
  30.  
  31.           $route->setVars($listVars);
  32.         }
  33.  
  34.         return $route;
  35.       }
  36.     }
  37.     throw new \RuntimeException('Aucune route ne correspond à l\'URL', self::NO_ROUTE);
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement