Example #1
0
 /**
  * Adds a route to the collection.
  *
  * The syntax used in the $route string depends on the used route parser.
  *
  * @param string|string[] $methods
  * @param string $route
  * @param mixed $handler
  * @return self
  */
 public function add($name, $methods, $pattern, $handler)
 {
     $route = new Route();
     if (!empty($name)) {
         $names = array_merge($this->groupNames, [$name]);
         $route->setName($names);
         $name = $route->getName();
         if (array_key_exists($name, $this->nameToRoute)) {
             throw new Exception("Route with name \"{$name}\" already exists");
         }
         $this->nameToRoute[$name] = $route;
     }
     $patterns = array_merge($this->groupPatterns, [$pattern]);
     $route->setPath($patterns);
     $route->setMethod($methods);
     $route->setHandler($handler);
     $routeDatas = $this->parse($route->getPath());
     foreach ($routeDatas as $routeData) {
         if ($this->isStaticRoute($routeData)) {
             $this->addStaticRoute($routeData, $route);
         } else {
             $this->addVariableRoute($routeData, $route);
         }
     }
     return $this;
 }
Example #2
0
 /**
  * @param $path string
  * @param $action callable|string
  */
 public static function post($path, $action)
 {
     $route = new Route();
     $route->setMethod("GET");
     $route->setPath($path);
     $route->setAction($action);
     self::addRoute($route);
 }
Example #3
0
 /**
  * Maps the route into routing table
  *
  * @param    Route   $route  A route instance
  * @return   Router  Returns current route instance
  */
 public function map(Route $route)
 {
     list($path, $groupMiddleware) = $this->processed;
     if ($path != '') {
         $route->setPath($path . $route->getPath());
     }
     if (!empty($groupMiddleware)) {
         $route->addMiddleware($groupMiddleware);
     }
     //Appends route to routing table.
     //We are using Trie search data structure.
     $this->routingTable->appendRoute($route);
     if ($route->getName() !== null) {
         //This is the named route
         if (isset($this->namedRoutes[$route->getName()])) {
             throw new \RuntimeException(sprintf("The Route with the name '%s' alredy exists", $route->getName()));
         } else {
             $this->namedRoutes[$route->getName()] = $route;
         }
     }
     return $this;
 }
Example #4
0
 public function testExtractingDynamicArguments()
 {
     $route = new Route();
     $route->setPath('/class/method/:id1/:id2');
     $route->addDynamicElement(':id1', ':id1');
     $route->addDynamicElement(':id2', ':id2');
     $route->matchMap('/class/method/one/two');
     $args_array = $route->getMapArguments();
     $this->assertArrayHasKey(':id1', $args_array);
     $this->assertArrayHasKey(':id2', $args_array);
 }
Example #5
0
 public function testSetPath()
 {
     $route = new Route("/");
     $route->setPath("/home");
     $this->assertEquals("/home", $route->getPath());
 }