Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Add original parameters to Route #27056

Merged
merged 2 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Illuminate/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ class Route
*/
public $compiled;

/**
* The matched parameters' original state.
*
* @var array
*/
protected $originalParameters;

/**
* The router instance used by the route.
*
Expand Down Expand Up @@ -300,6 +307,8 @@ public function bind(Request $request)
$this->parameters = (new RouteParameterBinder($this))
->parameters($request);

$this->originalParameters = $this->parameters;

return $this;
}

Expand Down Expand Up @@ -340,6 +349,18 @@ public function parameter($name, $default = null)
return Arr::get($this->parameters(), $name, $default);
}

/**
* Get original value of a given parameter from the route.
*
* @param string $name
* @param mixed $default
* @return string|object
*/
public function originalParameter($name, $default = null)
{
return Arr::get($this->originalParameters(), $name, $default);
}

/**
* Set a parameter to the given value.
*
Expand Down Expand Up @@ -383,6 +404,22 @@ public function parameters()
throw new LogicException('Route is not bound.');
}

/**
* Get the key / value list of original parameters for the route.
*
* @return array
*
* @throws \LogicException
*/
public function originalParameters()
{
if (isset($this->originalParameters)) {
return $this->originalParameters;
}

throw new LogicException('Route is not bound.');
}

/**
* Get the key / value list of parameters without null values.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,29 @@ public function testRoutePrefixing()
$this->assertEquals('prefix', $routes[0]->uri());
}

public function testRoutePreservingOriginalParametersState()
{
$phpunit = $this;
$router = $this->getRouter();
$router->bind('bar', function ($value) {
return strlen($value);
});
$router->get('foo/{bar}', [
'middleware' => SubstituteBindings::class,
'uses' => function ($bar) use ($router, $phpunit) {
$route = $router->getCurrentRoute();

$phpunit->assertEquals('taylor', $route->originalParameter('bar'));
$phpunit->assertEquals('default', $route->originalParameter('unexisting', 'default'));
$phpunit->assertEquals(['bar' => 'taylor'], $route->originalParameters());

return $bar;
},
]);

$this->assertEquals(6, $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
}

public function testMergingControllerUses()
{
$router = $this->getRouter();
Expand Down