There are 2 languages installed on Joomla 4 I was using for testing, English (the default one) and Romanian. When the articles and menus were set on English language or All languages there were everything ok, but when there was the Romanian language involved (everthing that came with domain-name.com/ro/wathever) the site was crashing with 404 page not found error because Router::parse was used in a plugin being developed. When the default language was changed from English to Romanian the problem persisted, but for the English language.
Analyzing the code I found the source of the error in the file JOOMLA_FOLDER/libraries/src/Router/Router.php: there were some path in the uri after parsing.
public function parse(&$uri, $setVars = false) { // Do the preprocess stage of the URL parse process $this->processParseRules($uri, self::PROCESS_BEFORE); // Do the main stage of the URL parse process $this->processParseRules($uri); // Do the postprocess stage of the URL parse process $this->processParseRules($uri, self::PROCESS_AFTER); // Check if all parts of the URL have been parsed. // Otherwise we have an invalid URL if (\strlen($uri->getPath()) > 0) { throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND')); } if ($setVars) { $this->setVars($uri->getQuery(true)); return $this->getVars(); } return $uri->getQuery(true); }
A solution I found is to use another function, parseRawRoute, when the error was triggered, with the same (good!) result.
$uri = JUri::getInstance();
$query = array(); $router = $app->getRouter(); try { $query = $router->parse($uri); // Get the real joomla query as an array - parse current joomla link } catch (Exception $e) { $query = $router->parseRawRoute($router, $tmpuri); // Get the real joomla query as an array - parse current joomla link }