Router::reverse PHP Method

reverse() public static method

Works similarly to Router::url(), but since parsed URL's contain additional 'pass' and 'named' as well as 'url.url' keys. Those keys need to be specially handled in order to reverse a params array into a string URL. This will strip out 'autoRender', 'bare', 'requested', and 'return' param names as those are used for CakePHP internals and should not normally be part of an output URL.
public static reverse ( CakeRequest | array $params, boolean $full = false ) : string
$params CakeRequest | array The params array or CakeRequest object that needs to be reversed.
$full boolean Set to true to include the full URL including the protocol when reversing the URL.
return string The string that is the reversed result of the array
    public static function reverse($params, $full = false)
    {
        if ($params instanceof CakeRequest) {
            $url = $params->query;
            $params = $params->params;
        } else {
            $url = $params['url'];
        }
        $pass = isset($params['pass']) ? $params['pass'] : array();
        $named = isset($params['named']) ? $params['named'] : array();
        unset($params['pass'], $params['named'], $params['paging'], $params['models'], $params['url'], $url['url'], $params['autoRender'], $params['bare'], $params['requested'], $params['return'], $params['_Token']);
        $params = array_merge($params, $pass, $named);
        if (!empty($url)) {
            $params['?'] = $url;
        }
        return Router::url($params, $full);
    }

Usage Example

 /**
  * Parses a string url into an array. Parsed urls will result in an automatic
  * redirection
  *
  * @param string $url The url to parse
  * @return boolean False on failure
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!$this->response) {
         $this->response = new CakeResponse();
     }
     $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 += array('named' => $params['named'], 'pass' => $params['pass'], 'url' => array());
         $redirect = Router::reverse($redirect);
     }
     $status = 301;
     if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
         $status = $this->options['status'];
     }
     $this->response->header(array('Location' => Router::url($redirect, true)));
     $this->response->statusCode($status);
     $this->response->send();
     $this->_stop();
 }
All Usage Examples Of Router::reverse