Ejemplo n.º 1
0
 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);
 }
Ejemplo n.º 2
0
 /**
  * Sets the subdirectory prefix for this route.
  *
  * @param string $subdirectory
  * @return $this
  */
 public function setSubdirectory($subdirectory)
 {
     $this->subdirectory = $subdirectory;
     $this->regexPattern = VariableUrl::createRegexMask($subdirectory . $this->pattern);
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * Dispatches a Route, executing its action.
  *
  * @param Route $route The route to be executed.
  * @param Request $request The request information, if available. Used for mapping route variables.
  * @return mixed Route target function return value, if any.
  */
 public function dispatch(Route $route, Request $request = null)
 {
     $context = $this->context;
     if (empty($this->context) && !empty($request)) {
         // If we have no context, but do have a request, prepare a context to store path variables in.
         // Otherwise routing path variables would be lost for no good reason.
         $context = new Context();
         $context->registerInstance($request);
     }
     if (!empty($context)) {
         // If we have a context, ensure that the route is made available in it.
         $context->registerInstance($route);
         if (!empty($request)) {
             // If we have a request, map the path variables and pass them to the context as primitive types by name.
             // This will allow us to inject info from a route e.g. "/view/$userId" to a $userId variable.
             $pathVariables = VariableUrl::extractUrlVariables($request->getRequestUri(), $route->getPattern());
             foreach ($pathVariables as $name => $value) {
                 $context->registerVariable($name, $value);
             }
         }
     }
     return $route->action($context);
 }