mirror of
https://github.com/flarum/core.git
synced 2025-08-09 18:07:02 +02:00
Throw error if required route params missing (#3118)
Co-authored-by: Daniël Klabbers <daniel@klabbers.email> Co-authored-by: luceos <luceos@users.noreply.github.com> Co-authored-by: David Wheatley <hi@davwheat.dev> Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
This commit is contained in:
committed by
GitHub
parent
4add23a984
commit
87f67744a8
@@ -121,11 +121,17 @@ class RouteCollection
|
||||
return $this->dataGenerator->getData();
|
||||
}
|
||||
|
||||
protected function fixPathPart(&$part, $key, array $parameters)
|
||||
protected function fixPathPart($part, array $parameters, string $routeName)
|
||||
{
|
||||
if (is_array($part) && array_key_exists($part[0], $parameters)) {
|
||||
$part = $parameters[$part[0]];
|
||||
if (! is_array($part)) {
|
||||
return $part;
|
||||
}
|
||||
|
||||
if (! array_key_exists($part[0], $parameters)) {
|
||||
throw new \InvalidArgumentException("Could not generate URL for route '$routeName': no value provided for required part '$part[0]'.");
|
||||
}
|
||||
|
||||
return $parameters[$part[0]];
|
||||
}
|
||||
|
||||
public function getPath($name, array $parameters = [])
|
||||
@@ -151,9 +157,11 @@ class RouteCollection
|
||||
}
|
||||
}
|
||||
|
||||
array_walk($matchingParts, [$this, 'fixPathPart'], $parameters);
|
||||
$fixedParts = array_map(function ($part) use ($parameters, $name) {
|
||||
return $this->fixPathPart($part, $parameters, $name);
|
||||
}, $matchingParts);
|
||||
|
||||
return '/'.ltrim(implode('', $matchingParts), '/');
|
||||
return '/'.ltrim(implode('', $fixedParts), '/');
|
||||
}
|
||||
|
||||
throw new \RuntimeException("Route $name not found");
|
||||
|
Reference in New Issue
Block a user