/** * Checks each route against the request and on match */ public static function proccess(Request $request = null) { static::init(); if (is_null($request)) { $request = Request::real(); } foreach (static::$routes as $name => $route) { if ($route["route"]->match($request)) { if ($route["closure"]) { call_user_func_array($route["closure"], array($route)); } elseif (Event::hasListeners("sugi.route")) { Event::fire("sugi.route", $route); } else { $r = $route["route"]; App::execute("Controller_{$r->get('controller')}", "action_{$r->get('action')}", array($r->get('param'))) and exit; } } } }
/** * Match defined route rules against the request * @param Sugi\HTTP\Request $request * @return boolean - true if the request match defined route */ public function match(Request $request = null) { // setting default values as a variables $this->variables = $this->defaults; if (is_null($request)) { $request = Request::real(); } if ($this->matchScheme($request->scheme()) === false) { return false; } if ($this->matchMethod($request->method()) === false) { return false; } if ($this->matchHost($request->host()) === false) { return false; } if ($this->matchPath($request->path()) === false) { return false; } return true; }
public function testCookies() { $req = Request::custom("", "GET", array(), array("cookiename" => "cookievalue", "foo" => "bar")); // cookies array $this->assertInternalType("array", $req->cookie->all()); // cookie $this->assertEquals("cookievalue", $req->cookie->get("cookiename")); // cookie2 $this->assertEquals("bar", $req->cookie->get("foo", "foobar")); // notexisting cookie $this->assertSame(null, $req->cookie->get("notexisting")); // defualt value $this->assertEquals("default", $req->cookie->get("notexistsing", "default")); }
public function testHostVariablesWithRequisites() { $route = new Route("/"); $route->setHost("{lang}.example.com"); $route->setRequisites(array("lang" => "en|bg")); // ok $this->assertTrue($route->match(Request::custom("http://en.example.com/"))); $this->assertTrue($route->match(Request::custom("http://bg.example.com/"))); // fail $this->assertFalse($route->match(Request::custom("http://example.com/"))); $this->assertFalse($route->match(Request::custom("http://.example.com/"))); $this->assertFalse($route->match(Request::custom("http://ru.example.com/"))); $this->assertFalse($route->match(Request::custom("http://ru.example.com/"))); $this->assertFalse($route->match(Request::custom("http://www.en.example.com/"))); $this->assertFalse($route->match(Request::custom("http://english.example.com/"))); // with default value $route->setDefaults(array("lang" => "en")); // ok $this->assertTrue($route->match(Request::custom("http://example.com/"))); // fails $this->assertFalse($route->match(Request::custom("http://ru.example.com/"))); }