Router::normalize PHP Method

normalize() public static method

Will strip the base path off and replace any double /'s. It will not unify the casing and underscoring of the input value.
public static normalize ( array | string $url = '/' ) : string
$url array | string URL to normalize Either an array or a string URL.
return string Normalized URL
    public static function normalize($url = '/')
    {
        if (is_array($url)) {
            $url = Router::url($url);
        }
        if (preg_match('/^[a-z\\-]+:\\/\\//', $url)) {
            return $url;
        }
        $request = Router::getRequest();
        if (!empty($request->base) && stristr($url, $request->base)) {
            $url = preg_replace('/^' . preg_quote($request->base, '/') . '/', '', $url, 1);
        }
        $url = '/' . $url;
        while (strpos($url, '//') !== false) {
            $url = str_replace('//', '/', $url);
        }
        $url = preg_replace('/(?:(\\/$))/', '', $url);
        if (empty($url)) {
            return '/';
        }
        return $url;
    }

Usage Example

 /**
  * Calls a controller's method from any location. Can be used to connect controllers together
  * or tie plugins into a main application. requestAction can be used to return rendered views
  * or fetch the return value from controller actions.
  *
  * Under the hood this method uses Router::reverse() to convert the $url parameter into a string
  * URL.  You should use URL formats that are compatible with Router::reverse()
  *
  * #### Passing POST and GET data
  *
  * POST and GET data can be simulated in requestAction.  Use `$extra['url']` for
  * GET data.  The `$extra['data']` parameter allows POST data simulation.
  *
  * @param string|array $url String or array-based url.  Unlike other url arrays in CakePHP, this
  *    url will not automatically handle passed and named arguments in the $url parameter.
  * @param array $extra if array includes the key "return" it sets the AutoRender to true.  Can
  *    also be used to submit GET/POST data, and named/passed arguments.
  * @return mixed Boolean true or false on success/failure, or contents
  *    of rendered action if 'return' is set in $extra.
  */
 public function requestAction($url, $extra = array())
 {
     if (empty($url)) {
         return false;
     }
     App::uses('Dispatcher', 'Routing');
     if (($index = array_search('return', $extra)) !== false) {
         $extra['return'] = 0;
         $extra['autoRender'] = 1;
         unset($extra[$index]);
     }
     if (is_array($url) && !isset($extra['url'])) {
         $extra['url'] = array();
     }
     $extra = array_merge(array('autoRender' => 0, 'return' => 1, 'bare' => 1, 'requested' => 1), $extra);
     $data = isset($extra['data']) ? $extra['data'] : null;
     unset($extra['data']);
     if (is_string($url) && strpos($url, FULL_BASE_URL) === 0) {
         $url = Router::normalize(str_replace(FULL_BASE_URL, '', $url));
     }
     if (is_string($url)) {
         $request = new CakeRequest($url);
     } elseif (is_array($url)) {
         $params = $url + array('pass' => array(), 'named' => array(), 'base' => false);
         $params = array_merge($params, $extra);
         $request = new CakeRequest(Router::reverse($params), false);
     }
     if (isset($data)) {
         $request->data = $data;
     }
     $dispatcher = new Dispatcher();
     $result = $dispatcher->dispatch($request, new CakeResponse(), $extra);
     Router::popRequest();
     return $result;
 }
All Usage Examples Of Router::normalize