Cake\Routing\Route\Route::parse PHP Method

parse() public method

If the route can be parsed an array of parameters will be returned; if not false will be returned. String URLs are parsed if they match a routes regular expression.
public parse ( string $url, string $method = '' ) : array | false
$url string The URL to attempt to parse.
$method string The HTTP method of the request being parsed.
return array | false An array of request parameters, or false on failure.
    public function parse($url, $method = '')
    {
        if (empty($this->_compiledRoute)) {
            $this->compile();
        }
        list($url, $ext) = $this->_parseExtension($url);
        if (!preg_match($this->_compiledRoute, urldecode($url), $route)) {
            return false;
        }
        if (isset($this->defaults['_method'])) {
            if (empty($method)) {
                // Deprecated reading the global state is deprecated and will be removed in 4.x
                $request = Router::getRequest(true) ?: Request::createFromGlobals();
                $method = $request->env('REQUEST_METHOD');
            }
            if (!in_array($method, (array) $this->defaults['_method'], true)) {
                return false;
            }
        }
        array_shift($route);
        $count = count($this->keys);
        for ($i = 0; $i <= $count; $i++) {
            unset($route[$i]);
        }
        $route['pass'] = [];
        // Assign defaults, set passed args to pass
        foreach ($this->defaults as $key => $value) {
            if (isset($route[$key])) {
                continue;
            }
            if (is_int($key)) {
                $route['pass'][] = $value;
                continue;
            }
            $route[$key] = $value;
        }
        if (isset($route['_args_'])) {
            $pass = $this->_parseArgs($route['_args_'], $route);
            $route['pass'] = array_merge($route['pass'], $pass);
            unset($route['_args_']);
        }
        if (isset($route['_trailing_'])) {
            $route['pass'][] = $route['_trailing_'];
            unset($route['_trailing_']);
        }
        if (!empty($ext)) {
            $route['_ext'] = $ext;
        }
        // restructure 'pass' key route params
        if (isset($this->options['pass'])) {
            $j = count($this->options['pass']);
            while ($j--) {
                if (isset($route[$this->options['pass'][$j]])) {
                    array_unshift($route['pass'], $route[$this->options['pass'][$j]]);
                }
            }
        }
        $route['_matchedRoute'] = $this->template;
        return $route;
    }

Usage Example

Example #1
0
 /**
  * Parses a string URL into an array. Parsed URLs will result in an automatic
  * redirection.
  *
  * @param string $url The URL to parse.
  * @param string $method The HTTP method being used.
  * @return false|null False on failure. An exception is raised on a successful match.
  * @throws \Cake\Routing\Exception\RedirectException An exception is raised on successful match.
  *   This is used to halt route matching and signal to the middleware that a redirect should happen.
  */
 public function parse($url, $method = '')
 {
     $params = parent::parse($url, $method);
     if (!$params) {
         return false;
     }
     $redirect = $this->redirect;
     if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
         $redirect = $this->redirect[0];
     }
     if (isset($this->options['persist']) && is_array($redirect)) {
         $redirect += ['pass' => $params['pass'], 'url' => []];
         if (is_array($this->options['persist'])) {
             foreach ($this->options['persist'] as $elem) {
                 if (isset($params[$elem])) {
                     $redirect[$elem] = $params[$elem];
                 }
             }
         }
         $redirect = Router::reverse($redirect);
     }
     $status = 301;
     if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
         $status = $this->options['status'];
     }
     throw new RedirectException(Router::url($redirect, true), $status);
 }
All Usage Examples Of Cake\Routing\Route\Route::parse