Ejemplo n.º 1
0
 /**
  * Parse a SEF URL path into URL parameters
  *
  * @param   string $path The path to parse, e.g. /foo/bar/1/2/3
  *
  * @return  array|null  The URL parameters or null if we can't parse the route with this rule
  */
 public function parse($path)
 {
     $extraParams = array();
     if (strpos($path, '?') !== false) {
         $uri = new Uri($path);
         $path = $uri->getPath();
         $extraParams = $uri->getQuery(true);
     }
     if ($this->useCallableForParse) {
         $ret = call_user_func($this->parseCallable, $path);
     } else {
         // Explode the path parts
         $segments = explode('/', $path);
         // Try to extract the URL parameters by parsing the segments
         $params = $this->parseRoute($segments);
         // If we got null back we can't parse this route
         if (is_null($params)) {
             return null;
         }
         // Mix in the push variables
         $params = array_merge($this->pushVars, $params);
         // Return the URL parameters
         $ret = $params;
     }
     return array_merge($ret, $extraParams);
 }
Ejemplo n.º 2
0
 /**
  * Parse a routed URL based on the routing rules, setting the input variables of the attached application.
  *
  * @param   string  $url    The URL to parse. If omitted the current URL will be used.
  * @param   boolean $rebase Is this a rebased URL? If false we assume we're given a relative URL.
  */
 public function parse($url = null, $rebase = true)
 {
     // If we are not given a URL, use the current URL of the site
     if (empty($url)) {
         $url = Uri::current();
     }
     // Initialise
     $removePath = null;
     $removeVars = null;
     if ($rebase) {
         // Get the base URL
         $baseUrl = $this->container->appConfig->get('base_url', '');
         if (empty($baseUrl)) {
             $baseUrl = '';
         }
         $baseUrl = rtrim($baseUrl, '/');
         $base = Uri::base(false, $this->container);
         $base = rtrim($base, '/') . '/' . $baseUrl;
         $rebaseURI = new Uri($base);
         // Get the path and vars to remove from the parsed route
         $removePath = $rebaseURI->getPath();
         $removePath = trim($removePath, '/');
         $removeVars = $rebaseURI->getQuery(true);
     }
     $uri = new Uri($url);
     $path = $uri->getPath();
     $path = trim($path, '/');
     // Remove the $removePath
     if (!empty($removePath)) {
         if (strpos($path, $removePath) === 0) {
             $path = substr($path, strlen($removePath));
         }
     }
     // Use the routing rules to parse the URL
     $routeVars = null;
     if (!empty($this->rules)) {
         /** @var Rule $rule */
         foreach ($this->rules as $rule) {
             $routeVars = $rule->parse($path);
             if (is_array($routeVars)) {
                 break;
             }
         }
     }
     if (is_null($routeVars)) {
         $routeVars = array();
     }
     // Mix route and URI vars
     $uriVars = $uri->getQuery(true);
     $routeVars = array_merge($routeVars, $uriVars);
     // Eliminate $removeVars
     if (is_array($removeVars) && !empty($removeVars)) {
         foreach ($removeVars as $k => $v) {
             if (isset($routeVars[$k]) && $routeVars[$k] == $v) {
                 unset($routeVars[$k]);
             }
         }
     }
     // Set the query vars to the application
     if (is_array($routeVars) && !empty($routeVars)) {
         $this->container->input->setData($routeVars);
     }
 }