Back to SiteRouter class

Method parseSefRoute

public void
parseSefRoute
(mixed &$router, mixed &$uri)
Convert a sef route to an internal URI
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 parseSefRoute - Source code

/**
 * Convert a sef route to an internal URI
 *
 * @param   SiteRouter  &$router  Router object
 * @param   Uri         &$uri     URI object to process
 *
 * @return  void
 *
 * @since   4.0.0
 */
public function parseSefRoute(&$router, &$uri)
{
    $route = $uri->getPath();
    // If the URL is empty, we handle this in the non-SEF parse URL
    if (empty($route)) {
        return;
    }
    // Parse the application route
    $segments = explode('/', $route);
    if (\count($segments) > 1 && $segments[0] === 'component') {
        $uri->setVar('option', 'com_' . $segments[1]);
        $uri->setVar('Itemid', null);
        $route = implode('/', \array_slice($segments, 2));
    } else {
        // Get menu items.
        $items = $this->menu->getItems(['parent_id', 'access'], [1, null]);
        $lang_tag = $this->app->getLanguage()->getTag();
        $found = null;
        foreach ($segments as $segment) {
            $matched = false;
            foreach ($items as $item) {
                if ($item->alias == $segment && (!$this->app->getLanguageFilter() || ($item->language === '*' || $item->language === $lang_tag))) {
                    $found = $item;
                    $matched = true;
                    $items = $item->getChildren();
                    break;
                }
            }
            if (!$matched) {
                break;
            }
        }
        // Menu links are not valid URLs. Find the first parent that isn't a menulink
        if ($found && $found->type === 'menulink') {
            while ($found->hasParent() && $found->type === 'menulink') {
                $found = $found->getParent();
            }
            if ($found->type === 'menulink') {
                $found = null;
            }
        }
        if (!$found) {
            $found = $this->menu->getDefault($lang_tag);
        } else {
            $route = trim(substr($route, \strlen($found->route)), '/');
        }
        if ($found) {
            if ($found->type === 'alias') {
                $newItem = $this->menu->getItem($found->getParams()->get('aliasoptions'));
                if ($newItem) {
                    $found->query = array_merge($found->query, $newItem->query);
                    $found->component = $newItem->component;
                }
            }
            $uri->setVar('Itemid', $found->id);
            $uri->setVar('option', $found->component);
        }
    }
    // Set the active menu item
    if ($uri->getVar('Itemid')) {
        $this->menu->setActive($uri->getVar('Itemid'));
    }
    // Parse the component route
    if (!empty($route) && $uri->getVar('option')) {
        $segments = explode('/', $route);
        if (\count($segments)) {
            // Handle component route
            $component = preg_replace('/[^A-Z0-9_\\.-]/i', '', $uri->getVar('option'));
            $crouter = $this->getComponentRouter($component);
            $uri->setQuery(array_merge($uri->getQuery(true), $crouter->parse($segments)));
        }
        $route = implode('/', $segments);
    }
    $uri->setPath($route);
}