示例#1
0
 /**
  * @param string $routeName уникальное имя маршрута.
  * @param array $params параметры.
  * @return string возвращает url адрес по имени маршрута и переданным параметрам.
  * @throws \InvalidArgumentException
  * @throws \LogicException
  */
 public function createUrl($routeName, array $params = [])
 {
     $route = $this->routes->get($routeName);
     if (!$route) {
         throw new \InvalidArgumentException(sprintf('Route with the name "%s" does not exist', $routeName));
     }
     $srcNames = $route->getParamNames();
     $names = array_keys($params);
     if (array_diff($srcNames, $names)) {
         throw new \LogicException('Missing required params');
     }
     $replacePairs = [];
     foreach ($params as $name => $value) {
         $replacePairs["{{$name}}"] = $value;
     }
     return strtr($route->getPattern(), $replacePairs);
 }
示例#2
0
 public function testMethods()
 {
     $router = new RouteCollection();
     $this->assertFalse($router->has("home"));
     $this->assertNull($router->get("home"));
     $this->assertSame(0, $router->count());
     // add one
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->add("home", new Route("/")));
     $this->assertSame(1, $router->count());
     $this->assertTrue($router->has("home"));
     $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home"));
     // change it
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->set("home", new Route("/foo")));
     $this->assertSame(1, $router->count());
     $this->assertTrue($router->has("home"));
     $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home"));
     // remove it
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->delete("home"));
     $this->assertFalse($router->has("home"));
     $this->assertNull($router->get("home"));
     $this->assertSame(0, $router->count());
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function getRouteCollection()
 {
     if (!$this->routes) {
         $this->routes = $this->controllers->getRoutes();
         foreach ($this->aliases as $source => $alias) {
             $name = $source;
             $params = [];
             if ($query = substr(strstr($source, '?'), 1)) {
                 $name = strstr($source, '?', true);
                 parse_str($query, $params);
             }
             if ($route = $this->routes->get($name)) {
                 $this->routes->add($source, new Route($alias[0], array_merge($route->getDefaults(), $params, ['_variables' => $route->compile()->getPathVariables()])));
             }
         }
     }
     return $this->routes;
 }
 public function testEnvironmentRestricts()
 {
     // ENVIRONMENT should be 'testing'
     $_SERVER['REQUEST_METHOD'] = 'GET';
     $routes = new RouteCollection();
     $expected = ['here' => '\\there'];
     $routes->environment('testing', function ($routes) {
         $routes->get('here', 'there');
     });
     $routes->environment('badenvironment', function ($routes) {
         $routes->get('from', 'to');
     });
     $this->assertEquals($expected, $routes->getRoutes());
 }