A bug in Route::constructUrl()

4 years ago

CARBON
Member | 1
+
0
-

Hi,
when building localized routes with global filterOut (in $metadata[NULL]), I have found a bug in \Nette\Application\Routers\Route::constructUrl().

If the filterOut function transforms the $params array and changes $params[‘presenter’] to an another value, next part of code resets it back to the previous value when trying to split into module and presenter name. It does not seems correct to me.

Problem explained in comments:

	$presenter = $appRequest->getPresenterName();
	$params[self::PRESENTER_KEY] = $presenter; // there $params['presenter'] == 'App:Login'

	if (isset($metadata[NULL][self::FILTER_OUT])) {
		$params = call_user_func($metadata[NULL][self::FILTER_OUT], $params);
		// after this, values were transformed, so $params['presenter'] == 'App:Prihlaseni'
		if ($params === NULL) {
			return NULL;
		}
	}

	// there should be fix like this: $presenter = $params['presenter'];

	if (isset($metadata[self::MODULE_KEY])) { // try split into module and [submodule:]presenter parts
		$module = $metadata[self::MODULE_KEY];
		if (isset($module['fixity']) && strncmp($presenter, $module[self::VALUE] . ':', strlen($module[self::VALUE]) + 1) === 0) {
			$a = strlen($module[self::VALUE]);
		} else {
			$a = strrpos($presenter, ':');
		}
		if ($a === FALSE) {
			$params[self::MODULE_KEY] = isset($module[self::VALUE]) ? '' : NULL;
		} else {
			$params[self::MODULE_KEY] = substr($presenter, 0, $a);
			$params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
		}
	}

	// condition above resets presenter's name, so $params['presenter'] == 'App:Login' again...

According to me, variable $presenter should be updated to value of $params[‘presenter’] after filterOut function has been called. Otherwise, it is useless trying to modify it.