/** * Creates and registers a new redirection route. * * @param string $from The route mask to match against. Supports variables. * @param string $to The static URL to redirect the user to. Supports variables. * @param bool $permanent If true, a HTTP 301 permanent redirect is used. Otherwise, a HTTP 302 temporary redirect is used (default). * @return Route Returns the Route that was created and registered. */ public function createRedirect($from, $to, $permanent = false) { $route = new Route($from, function (Request $request, Response $response) use($from, $to, $permanent) { // Map URL variables $urlVariables = VariableUrl::extractUrlVariables($request->getRequestUri(), $to); if (!empty($urlVariables)) { $to = VariableUrl::applyUrlVariables($to, $urlVariables); } // Perform redirect response $response->doRedirect($to, $permanent); }); $this->register($route); return $route; }
public function testComplexExternalUrlMapping() { $routePatten = '/test/local/$testvar/$why/$also'; $requestUri = '/test/local/bla/whynot/hi'; $targetUri = 'https://www.google.com/search?q=$testvar&why=$why&also=$also'; $route = new Route($routePatten, function () { // ... }); $request = new Request(); $request->setRequestUri($requestUri); $this->assertTrue($route->matches($request)); $variableSet = VariableUrl::extractUrlVariables($requestUri, $routePatten); $this->assertEquals(['testvar' => 'bla', 'why' => 'whynot', 'also' => 'hi'], $variableSet); $mappedOutput = VariableUrl::applyUrlVariables($targetUri, $variableSet); $this->assertEquals('https://www.google.com/search?q=bla&why=whynot&also=hi', $mappedOutput); }