Back to SiteRouter class

Method parseInit

public void
parseInit
(mixed &$router, mixed &$uri)
Do some initial cleanup before parsing the URL
Parameters
  • \Joomla\CMS\Router\SiteRouter & $router Router object
  • \Joomla\CMS\Uri\Uri & $uri URI object to process
Returns
  • void
Since
  • 4.0.0
Class: SiteRouter
Project: Joomla

Method parseInit - Source code

/**
 * Do some initial cleanup before parsing the URL
 *
 * @param   SiteRouter  &$router  Router object
 * @param   Uri         &$uri     URI object to process
 *
 * @return  void
 *
 * @since   4.0.0
 */
public function parseInit(&$router, &$uri)
{
    // Get the path
    // Decode URL to convert percent-encoding to unicode so that strings match when routing.
    $path = urldecode($uri->getPath());
    /**
     * In some environments (e.g. CLI we can't form a valid base URL). In this case we catch the exception thrown
     * by URI and set an empty base URI for further work.
     * @todo: This should probably be handled better
     */
    try {
        $baseUri = Uri::base(true);
    } catch (\RuntimeException $e) {
        $baseUri = '';
    }
    // Remove the base URI path.
    $path = substr_replace($path, '', 0, \strlen($baseUri));
    // Check to see if a request to a specific entry point has been made.
    if (preg_match("#.*?\\.php#u", $path, $matches)) {
        // Get the current entry point path relative to the site path.
        $scriptPath = realpath($_SERVER['SCRIPT_FILENAME'] ?: str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']));
        $relativeScriptPath = str_replace('\\', '/', str_replace(JPATH_SITE, '', $scriptPath));
        // If a php file has been found in the request path, check to see if it is a valid file.
        // Also verify that it represents the same file from the server variable for entry script.
        if (is_file(JPATH_SITE . $matches[0]) && $matches[0] === $relativeScriptPath) {
            // Remove the entry point segments from the request path for proper routing.
            $path = str_replace($matches[0], '', $path);
        }
    }
    // Set the route
    $uri->setPath(trim($path, '/'));
}